Reputation: 91
I try to convert NSString
to int
,Result dpPoint:0, I want dpPoint:2
dpPointStr = [NSString stringWithFormat:@"%@",[verifyRow valueForKey:@"default_point"]];
NSLog(@"dpPointStr:%@",dpPointStr); //Result dpPointStr:2
int dpPoint = [dpPointStr intValue];
NSLog(@"dpPoint:%i",dpPoint); //Result dpPoint:0
Upvotes: 2
Views: 10203
Reputation: 71
Convert NSString to integer
NSString *sampleText = @"5";
int numValue = [sampleText intValue];
NSLog(@"Integer Value: %d", numValue);
Upvotes: -1
Reputation: 877
In your case, if value is in the beginning/end of the string you can try this:
int val = [[dpPointStr stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] intValue];
Upvotes: 4
Reputation: 6039
int str_len = [yourStr length]; char tmp[str_len];
[NSString getCString:tmp maxLength:str_len encoding:UTF8Encoding];
int dpPoint = atoi(tmp);
Don't forget to include this: #include
I think this should work just fine.
Also, I think you can fix this by taking the intValue from the:
[verifyRow valueForKey:@"default_point"];
To be consistent with Apple use NSInteger instead, or even better NSNumber for the valueForKey statement and then NSInteger.
Upvotes: 0