Objective-C 傻瓜书:如何从 NSDictionary 中获取值?

发布于 2024-10-15 05:35:45 字数 502 浏览 5 评论 0原文

所以我从 NSDictionary 中提取值是最困难的。现在我只有一个从 JSON 调用填充的字典,它只包含一个名为“Success”、值为 0 或 1 的键。

如何对该值执行条件来检查它是 0 还是 1?我尝试了很多事情,但一无所获。这是我当前的代码:

[[jsonDictionary objectForKey:@"Success"] isEqualToNumber:1]

我收到 passing argument 1 of 'isEqualToNumber:' madepointer from integer without a cast' 作为警告,无论如何,应用程序在遇到该行时都会崩溃。

还有一个子问题,objectForKeyvalueForKey 之间有什么区别?我应该默认使用哪一个?

不管怎样,这个 Objective-C 菜鸟真的很感激在这方面的一些帮助。提前致谢!

so I'm having the most difficult of time pulling values out of an NSDictionary. Right now I just have a dictionary that is populated from a JSON call and it only contains a key named 'Success' with a value of 0 or 1.

How do I do a conditional on that value to check if its 0 or 1? I've tried a bunch of things, but I'm not getting anywhere. Here's my current code:

[[jsonDictionary objectForKey:@"Success"] isEqualToNumber:1]

I'm getting passing argument 1 of 'isEqualToNumber:' makes pointer from integer without a cast' as a warning, and the app crashes when it hits that line anyway.

And a subquestion, what's the difference between objectForKey and valueForKey? Which one should I use by default?

Anyway, this noob in Objective-C would truly appreciate some help on this. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

虫児飞 2024-10-22 05:35:45

由于字典包含 Objective-C 对象,因此包含数字的条目是一个 NSNumber 实例。 NSNumber 提供了一个方便的方法 -intValue,用于提取其底层 int 值:

if ([[jsonDictionary objectForKey:@"Success"] intValue] == 1) { … }

请注意,NSNumber 还有其他便利提取其底层值作为其他 C 数据类型的方法。


在大多数情况下,您应该使用 -objectForKey: 而不是 -valueForKey: 前者是获取字典中条目的规范方法并在 NSDictionary 中声明。后者在 NSObject 中声明,并在键值编码上下文中使用,其中密钥必须是有效的 KVC 密钥,并且有额外的处理 - 例如,如果您使用 - valueForKey: 在具有以 @ 开头的键的字典中,该字符将从键中删除并调用 [super valueForKey:key]

Since dictionaries contain Objective-C objects, an entry containing a number is an NSNumber instance. NSNumber provides a convenience method, -intValue, for extracting its underlying int value:

if ([[jsonDictionary objectForKey:@"Success"] intValue] == 1) { … }

Note that NSNumber has other convenience methods for extracting its underlying value as other C data types.


In most cases, you should use -objectForKey: instead of -valueForKey:. The former is the canonical method to obtain an entry in the dictionary and is declared in NSDictionary. The latter is declared in NSObject and is used in Key-Value Coding contexts, where the key must be a valid KVC key, and there’s additional processing — for instance, if you’re using -valueForKey: in a dictionary with a key that starts with @, that character is stripped from the key and [super valueForKey:key] is called.

奢华的一滴泪 2024-10-22 05:35:45

数字 1 不是对象指针。如果您想在 NSDictionary 中使用数字,请使用 NSNumber 实例。

[[jsonDictionary objectForKey:@"Success"]
 isEqualToNumber:[NSNumber numberWithInteger:1]]

The number 1 is not an object pointer. Use an NSNumber instance instead if you want to use a number in an NSDictionary.

[[jsonDictionary objectForKey:@"Success"]
 isEqualToNumber:[NSNumber numberWithInteger:1]]
顾挽 2024-10-22 05:35:45
 [[jsonDictionary objectForKey:@"Success"] isEqualToNumber: [NSNumber numberWithInt:1]]
 [[jsonDictionary objectForKey:@"Success"] isEqualToNumber: [NSNumber numberWithInt:1]]
花之痕靓丽 2024-10-22 05:35:45

您可以通过不同的方式获取字典的值,例如检查
价值第一。

Solution 1: Using simple if statement.
int value = 0;
if ([[jsonDictionary objectForKey:@"Success"]intValue]==1){
    value = [[jsonDictionary objectForKey:@"Success"]intValue];
}

Solution 2: Using ternary operator
value = ([[jsonDictionary objectForKey:@"Success"]intValue]==1) ? 1:0;

You can get the value of dictionary in different ways like checking
the value first.

Solution 1: Using simple if statement.
int value = 0;
if ([[jsonDictionary objectForKey:@"Success"]intValue]==1){
    value = [[jsonDictionary objectForKey:@"Success"]intValue];
}

Solution 2: Using ternary operator
value = ([[jsonDictionary objectForKey:@"Success"]intValue]==1) ? 1:0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文