本文介绍了将NSInteger变量传递给NSMutableDictionary或NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么这不起作用:
NSInteger temp = 20;
[userSettingsFromFile setObject:temp forKey:@"aTemp"];
但是这样做:
[userSettingsFromFile setObject:@"someObject" forKey:@"aTemp"];
如何使用NSInteger
变量?
推荐答案
NSInteger
不是对象,它只是类型转换为32位的int
或64位的long
.由于NSDictionary
只能存储对象,因此需要先将整数包装到对象中,然后才能存储它.试试这个:
NSInteger
isn't an object -- it's simply typecast to int
on 32-bit or long
on 64-bit. Since NSDictionary
can only store objects, you need to wrap the integer into an object before you can store it. Try this:
NSInteger temp = 20;
[userSettingsFromFile setObject:[NSNumber numberWithInteger:temp]
forKey:@"aTemp"];
这篇关于将NSInteger变量传递给NSMutableDictionary或NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!