Convert NSString to NSInteger?

2020-01-27 01:55发布

I want to convert string data to NSInteger.

7条回答
Rolldiameter
2楼-- · 2020-01-27 02:04
NSNumber *tempVal2=[[[NSNumberFormatter alloc] init] numberFromString:@"your text here"];

returns NULL if string or returns NSNumber

NSInteger intValue=[tempVal2 integerValue];

returns integer of NSNumber

查看更多
干净又极端
3楼-- · 2020-01-27 02:04
int myInt = [myString intValue];
NSLog(@"Display Int Value:%i",myInt);
查看更多
神经病院院长
4楼-- · 2020-01-27 02:09

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

NSInteger myInt = [myString intValue];
查看更多
可以哭但决不认输i
5楼-- · 2020-01-27 02:10

[myString intValue] returns a cType "int"

[myString integerValue] returns a NSInteger.

In most cases I do find these simple functions by looking at apples class references, quickest way to get there is click [option] button and double-click on the class declarations (in this case NSString ).

查看更多
Juvenile、少年°
6楼-- · 2020-01-27 02:10

I've found this to be the proper answer.

NSInteger myInt = [someString integerValue];
查看更多
Animai°情兽
7楼-- · 2020-01-27 02:13
NSString *string = [NSString stringWithFormat:@"%d", theinteger];
查看更多
登录 后发表回答