将 int32_t 存储在 NSDictionary 中
如何将 int32_t
类型的变量(例如 ABPropertyID)存储在 NSDictionary
中?
[NSNumber numberWithInt:...]
似乎不起作用。
谢谢
来自评论:
NSLog(@" %@ %@ ", [NSNumber numberWithLong:kABPersonFirstNameProperty], kABPersonFirstNameProperty);
打印:0(空)有什么想法吗?
How can I store a variable of type int32_t
(e.g. for ABPropertyID) in an NSDictionary
?
[NSNumber numberWithInt:...]
doesn't seem to work.
Thanks
From the comments:
NSLog(@" %@ %@ ", [NSNumber numberWithLong:kABPersonFirstNameProperty], kABPersonFirstNameProperty);
Prints: 0 (null) Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
+[NSNumber numberWithInteger:]
将在所有 32 位和 64 位系统上很好地保存 32 位数字。+[NSNumber integerValue]
将检索它。如果你需要它未签名,你可以使用“+[NSNumber numberWithUnsignedInteger:]”。+[NSNumber numberWithInteger:]
will hold a 32-bit number nicely on all 32-bit and 64-bit systems.+[NSNumber integerValue]
will retrieve it. If you need it unsigned you can use ``+[NSNumber numberWithUnsignedInteger:]`.我在这里假设戴夫的问题特定于 kAB... 常量 - 在这种情况下我可能会错误地忽略以下内容:)
我相信这里的问题不是您使用了不正确的语法;而是您使用了错误的语法。这是一个初始化顺序问题。我猜测您在初始化 kAB... 常量之前尝试此操作 - 例如,通过调用
ABAddressBookCreate()
。有点令人困惑的问题是代码片段:NSLog(@" %@ %@ ", [NSNumber numberWithLong:kABPersonFirstNameProperty], kABPersonFirstNameProperty);
应该是:
NSLog(@" %@ %d ", [NSNumber numberWithLong:kABPersonFirstNameProperty], kABPersonFirstNameProperty);
这是我的输出 - 在调用
ABAddressBookCreate()
之前:NSLog(@"Check: %d %d %d ", kABPersonFirstNameProperty,kABPersonMiddleNameProperty,kABPersonLastNameProperty);
产生
调用后,产生相同的日志语句
I assume here that Dave's issue is specific to the kAB... constants--I may be mistaken in which case ignore the following :)
I believe that the issue here is not that you are using incorrect syntax; it's an order-of-initialization problem. I'm guessing that you are attempting this operation before having initialized the kAB... constants -- e.g., by calling
ABAddressBookCreate()
. Somewhat confusing the issue is the snippet:NSLog(@" %@ %@ ", [NSNumber numberWithLong:kABPersonFirstNameProperty], kABPersonFirstNameProperty);
which should be:
NSLog(@" %@ %d ", [NSNumber numberWithLong:kABPersonFirstNameProperty], kABPersonFirstNameProperty);
Here's my output -- prior to calling
ABAddressBookCreate()
:NSLog(@"Check: %d %d %d", kABPersonFirstNameProperty,kABPersonMiddleNameProperty,kABPersonLastNameProperty);
produces
after the call, the same log statement produces
正如其他人所说,
NSNumber
将适用于此。但是,您至少应该模糊地了解其他两个选项: CFDictionary(与底层的 NSDictionary 相同,但它允许您存储任意指针或指针) - 大小的整数)和 NSMapTable。As everyone else has said,
NSNumber
will work for this. However, there are two other options you should be at least vaguely aware of:CFDictionary
(the same thing asNSDictionary
under the hood, but it lets you store arbitrary pointers or pointer-sized integers) andNSMapTable
.在这种情况下,使用 NSNumber 似乎是最好的主意。
对于更困难的情况,您始终可以使用 NSValue 或 NSData 将任何类型或指针放入可存储在 Cocoa 集合中的 Objective-C 对象中。
In this case, using NSNumber seems like the best idea.
For harder cases, you can always use NSValue or NSData to put any type or pointer into a Objective-C object that can be stored in Cocoa collections.