类型为long的NSNumber对象

时间:2014-02-15 11:54:06

标签: ios objective-c int nsnumber

我遇到了NSNumber的一个奇怪的问题,当我运行以下代码时,

NSNumber *num = [NSNumber numberWithLong:1000];
const char* type =[num objCType];
if (strcmp (type, @encode (long)) == 0) {
    NSLog(@"Type is long");
}else if(strcmp (type, @encode (int)) == 0){
       NSLog(@"Type is int");
}

输出结果为:

  

Type is int

修改:我尝试了[NSNumber numberWithLong:1000L][NSNumber numberWithLong:1000000000]

但仍然是相同的输出。

为什么类型int在这里?

2 个答案:

答案 0 :(得分:3)

答案很简单...... objCType的documentation说:

  

特别注意事项

     

返回的类型不一定与接收者的方法匹配   是用。创建的。

答案 1 :(得分:0)

这就像要求stringWithInt方法返回仍然知道它是int的字符串。您创建NSNumber,即可获得NSNumber

相关问题