我有一个带有值的 NSMutableDictionary。其中一个值是 NSString“1”。
我的理解是这样的:
NSString *currentCount = [perLetterCount valueForKey:firstLetter];
然后我将其转换为 int:
int newInt = (int)currentCount;
我像这样显示两者:
NSLog(@"s: %@, i: %i", currentCount, newInt);
我得到这个结果:
c: 1, i: 156112
我做错了什么?
谢谢
最佳答案
您所做的是将指针(字符串对象 currentCount 的数据存储的地址)转换为整数。看来整数是156112。
要获取 NSString 的数值,您需要调用其值方法之一:
[ currentCount intValue ] or currentCount.intValue // for an int;
[ currentCount integerValue ] or currentCount.integerValue // for an NSInteger;
[ currentCount floatValue ] or currentCount.floatValue // for a float, and so on.
关于objective-c - 字符串到 int 的转换返回 156112 而不是 objective-c 中的 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10115367/