如何从NSDictionary获取密钥对象?

时间:2021-06-26 13:49:36

In dictionary named dict the key value pair is:

在名为dict的字典中,键值对是:

"alba\U2019s window" = "A model in westindies.";

“alba \ U2019s window”=“westindies的模特。”;

And to send objectForKey: I am getting the string like "alba's window". When I send like following:

并发送objectForKey:我得到的字符串就像“alba的窗口”。当我发送如下:

[dict objectForKey:alba's window];

[dict objectForKey:alba的窗口];

I am not getting anything, it is showing null.

我没有得到任何东西,它显示为null。

3 个解决方案

#1


16  

For starters, you might want to make that an actual string, like so:

对于初学者,您可能希望将其设为实际字符串,如下所示:

[dict objectForKey:@"alba's window"];

Note also that \U2019 is not '; but , which is a different character entirely.

还要注意\ U2019不是';但',这完全是一个不同的角色。

And more generally, sticking to [a-zA-Z0-9]+ for dictionary keys is probably a good idea, unless you are inserting and retrieving programmatically using the exact same string as a key.

更一般地说,坚持使用字典键的[a-zA-Z0-9] +可能是一个好主意,除非您使用与键完全相同的字符串以编程方式插入和检索。

#2


5  

Since iOS6 onwards, a convenient method for setting and accessing the object for a key from an NSDictionary is:

从iOS6开始,从NSDictionary设置和访问密钥对象的便捷方法是:

//Setting the object in NSMutableDictionary
dictionary[@"someKey"] = @"someString";

//Accessing the object
NSString *str = dictionary[@"someKey"];

#3


2  

Make sure your dict isn't null; sending a message to a null object will silently fail and return null.

确保你的dict不为null;向空对象发送消息将无提示失败并返回null。

Another way to check your key/values is to simply NSLog(@"%@",dict); and this will show you the contents of the dictionary. Note that this output only shows quotes around values when the value contains a space.

另一种检查键/值的方法是简单地使用NSLog(@“%@”,dict);这将显示字典的内容。请注意,此值仅在值包含空格时显示值周围的引号。

Also, make sure you're using the same pairs of strings as the key - it looks like you're using "alba\U2019s window" in addition to "alba's window".

此外,请确保您使用与键相同的字符串对 - 看起来除了“alba的窗口”之外,您还在使用“alba \ U2019s窗口”。

#1


16  

For starters, you might want to make that an actual string, like so:

对于初学者,您可能希望将其设为实际字符串,如下所示:

[dict objectForKey:@"alba's window"];

Note also that \U2019 is not '; but , which is a different character entirely.

还要注意\ U2019不是';但',这完全是一个不同的角色。

And more generally, sticking to [a-zA-Z0-9]+ for dictionary keys is probably a good idea, unless you are inserting and retrieving programmatically using the exact same string as a key.

更一般地说,坚持使用字典键的[a-zA-Z0-9] +可能是一个好主意,除非您使用与键完全相同的字符串以编程方式插入和检索。

#2


5  

Since iOS6 onwards, a convenient method for setting and accessing the object for a key from an NSDictionary is:

从iOS6开始,从NSDictionary设置和访问密钥对象的便捷方法是:

//Setting the object in NSMutableDictionary
dictionary[@"someKey"] = @"someString";

//Accessing the object
NSString *str = dictionary[@"someKey"];

#3


2  

Make sure your dict isn't null; sending a message to a null object will silently fail and return null.

确保你的dict不为null;向空对象发送消息将无提示失败并返回null。

Another way to check your key/values is to simply NSLog(@"%@",dict); and this will show you the contents of the dictionary. Note that this output only shows quotes around values when the value contains a space.

另一种检查键/值的方法是简单地使用NSLog(@“%@”,dict);这将显示字典的内容。请注意,此值仅在值包含空格时显示值周围的引号。

Also, make sure you're using the same pairs of strings as the key - it looks like you're using "alba\U2019s window" in addition to "alba's window".

此外,请确保您使用与键相同的字符串对 - 看起来除了“alba的窗口”之外,您还在使用“alba \ U2019s窗口”。