Convert NSString to NSInteger?

I want to convert string data to NSInteger .

+65
iphone nsstring nsinteger
Jan 25 2018-11-11T00:
source share
7 answers

If the string is a human readable number, you can do this:

 NSInteger myInt = [myString intValue]; 
+130
Jan 25 2018-11-11T00:
source share

[myString intValue] returns cType "int"

[myString integerValue] returns a NSInteger .

In most cases, I find these simple functions by looking at links to apple classes, the fastest way to get the [option] button pressed and double-click class declarations (in this case NSString).

+72
Jan 25 2018-11-11T00:
source share

I found this to be the correct answer.

 NSInteger myInt = [someString integerValue]; 
+19
Dec 23 '13 at 18:06
source share
 NSNumber *tempVal2=[[[NSNumberFormatter alloc] init] numberFromString:@"your text here"]; 

returns NULL if string or returns NSNumber

 NSInteger intValue=[tempVal2 integerValue]; 

returns an integer NSNumber

+6
Nov 26 '15 at 9:48
source share
 int myInt = [myString intValue]; NSLog(@"Display Int Value:%i",myInt); 
+2
Mar 18 '13 at 8:23
source share

it is safer than integerValue:

 -(NSInteger)integerFromString:(NSString *)string { NSNumberFormatter *formatter=[[NSNumberFormatter alloc]init]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; NSNumber *numberObj = [formatter numberFromString:string]; return [numberObj integerValue]; } 
0
Nov 24 '16 at 14:12
source share
 NSString *string = [NSString stringWithFormat:@"%d", theinteger]; 
-four
Mar 25 '15 at 11:19
source share



All Articles