NSNumber、设置和检索
我正在为 iPhone 应用程序使用 NSNumber,看看我能用它做什么。对于我的大多数变量,我简单地将它们存储为“int”或“float”或其他类型。但是,当我必须传递一个对象(例如在字典中)时,我需要它们作为对象。我使用 NSNUMer。这就是我初始化对象的方式。
NSNumber *testNum = [NSNumber numberWithInt:varMoney];
其中“varMoney”是我在程序前面声明的一个整数。但是,我完全不知道如何取回该数字......
例如:
varMoney2 = [NSNumber retrieve the variable...];
如何从对象取回值并将其再次设置为常规“int”?
谢谢!
(出于好奇,有没有办法将“int”直接存储在 Objective-C 字典中,而不先将其放入 NSNumber 中?)
I'm messing around with NSNumber for an iPhone app, and seeing what I can do with it. For most of my variables, I simple store them as "int" or "float" or whatnot. However, when I have to pass an object (Such as in a Dictionary) then I need them as an Object. I use NSNUmber. This is how I initialize the object.
NSNumber *testNum = [NSNumber numberWithInt:varMoney];
Where "varMoney" is an int I have declared earlier in the program. However, I have absolutely no idea how to get that number back...
for example:
varMoney2 = [NSNumber retrieve the variable...];
How do I get the value back from the object and set it to a regular "int" again?
Thanks!
(Out of curiosity, is there a way to store "int" directly in an Objective-C dictionary without putting it in an NSNumber first?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要 -intValue 或其朋友之一(-floatValue、-doubleValue 等)。来自文档:
代码是:
You want -intValue, or one of its friends (-floatValue, -doubleValue, etc.). From the docs:
The code would be: