Convert NSString to NSInteger?

Deadly 提交于 2019-12-17 07:14:03

问题


I want to convert string data to NSInteger.


回答1:


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

NSInteger myInt = [myString intValue];



回答2:


[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 ).




回答3:


I've found this to be the proper answer.

NSInteger myInt = [someString integerValue];



回答4:


NSNumber *tempVal2=[[[NSNumberFormatter alloc] init] numberFromString:@"your text here"];

returns NULL if string or returns NSNumber

NSInteger intValue=[tempVal2 integerValue];

returns integer of NSNumber




回答5:


int myInt = [myString intValue];
NSLog(@"Display Int Value:%i",myInt);



回答6:


this is safer than integerValue:

-(NSInteger)integerFromString:(NSString *)string
{
    NSNumberFormatter *formatter=[[NSNumberFormatter alloc]init];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber *numberObj = [formatter numberFromString:string];
    return [numberObj integerValue];
}



回答7:


NSString *string = [NSString stringWithFormat:@"%d", theinteger];


来源:https://stackoverflow.com/questions/4791470/convert-nsstring-to-nsinteger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!