将 int32_t 存储在 NSDictionary 中

发布于 2024-08-19 09:57:44 字数 326 浏览 11 评论 0原文

如何将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫﹏色ふ单纯 2024-08-26 09:57:44

+[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:]`.

時窥 2024-08-26 09:57:44

我在这里假设戴夫的问题特定于 kAB... 常量 - 在这种情况下我可能会错误地忽略以下内容:)

我相信这里的问题不是您使用了不正确的语法;而是您使用了错误的语法。这是一个初始化顺序问题。我猜测您在初始化 kAB... 常量之前尝试此操作 - 例如,通过调用 ABAddressBookCreate()。有点令人困惑的问题是代码片段:

NSLog(@" %@ %@ ", [NSNumber numberWithLong:kABPersonFirstNameProperty], kABPersonFirstNameProperty);

应该是:

NSLog(@" %@ %d ", [NSNumber numberWithLong:kABPersonFirstNameProperty], kABPersonFirstNameProperty);

这是我的输出 - 在调用 ABAddressBookCreate() 之前:

NSLog(@"Check: %d %d %d ", kABPersonFirstNameProperty,kABPersonMiddleNameProperty,kABPersonLastNameProperty);

产生

检查:0 0 0

调用后,产生相同的日志语句

检查:0 6 1

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

Check: 0 0 0

after the call, the same log statement produces

Check: 0 6 1

揽月 2024-08-26 09:57:44

正如其他人所说,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 as NSDictionary under the hood, but it lets you store arbitrary pointers or pointer-sized integers) and NSMapTable.

眼角的笑意。 2024-08-26 09:57:44

在这种情况下,使用 NSNumber 似乎是最好的主意。

对于更困难的情况,您始终可以使用 NSValue 或 NSData 将任何类型或指针放入可存储在 Cocoa 集合中的 Objective-C 对象中。

int32_t myInt = 42;
NSValue *myValue = [NSValue value:&myInt withObjCType:@encode(int32_t)];

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.

int32_t myInt = 42;
NSValue *myValue = [NSValue value:&myInt withObjCType:@encode(int32_t)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文