简体   繁体   English

如何从NSDictionary获取key @int的值?

[英]How to get the value for key @int from NSDictionary?

I am Creating a NSDictionary and adding a key value pair as below 我正在创建一个NSDictionary并添加一个键值对,如下所示

  NSDictionary* dictionary = @{ @0: @"I am the value" };

and retrieving the value as below 并检索如下值

  NSString* value = [dictionary valueForKey:@0];

Application crashed for doing this, I don't understand the reason, I am giving the same data type and value. 应用程序因执行该操作而崩溃,我不明白原因,我给出的数据类型和值相同。

What is the datatype of @0, I guess it is NSNumber, If not correct me. @ 0的数据类型是什么,我想是NSNumber,如果不正确的话。

Yes, it's NSNumber type however the problem is somewhere else. 是的,它是NSNumber类型,但是问题出在别的地方。

You should call objectForKey instead of valueForKey which is mainly used for KVO . 您应该调用objectForKey而不是主要用于KVOvalueForKey

NSString *value = [dictionary objectForKey:@0];

or better: 或更好:

NSString *value = dictionary[@0];

valueForKey: is not the proper method for what you are trying to achieve. valueForKey:不是您要实现的正确方法。

you should use: 您应该使用:

NSString* value = [dictionary objectForKey:@0];

ValueForKey is for key value coding and expect a NSString as parameters ValueForKey用于键值编码,并且期望使用NSString作为参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM