This Objective-C tutorial will show you how to convert a string (NSString) to different number types. The NSString class in Objective-C has a parse method for each number type that allows you convert a string into the primitive type.
Convert a NSString to an int
To convert a NSString to an int, call the method integerValue on the NSString class. Below is an example:NSString *str = @"123";
Convert a NSString to a long
int value = [str integerValue];
To convert a NSString to a long, call the method longLongValue on the NSString class. Below is an example:NSString *str = @"123";
long value = [str longLongValue];
Convert a NSString to a float
To convert a NSString to a float, call the method floatValue on the NSString class. Below is an example:NSString *str = @"123.4";
float value = [str floatValue];
Convert a NSString to a double
To convert a NSString to a double, call the method doubleValue on the NSString class. Below is an example:NSString *str = @"123.4";
double value = [str doubleValue];
0 comments:
Post a Comment