问题描述
我想在我的 NSDictionary
上使用 valueForKeyPath
,但问题是其中一个键是一个字符串以@符号开头。我无法控制密钥的命名。
I want to use valueForKeyPath
on my NSDictionary
, but the problem is that one of the keys is a string that starts with the @ symbol. I have no control over the naming of the key.
我在尝试创建密钥路径时遇到问题,因为我收到格式异常,即使在尝试逃避@符号:
I'm having problems trying to create the key path as I'm getting a format exception, even when trying to escape the @ symbol:
这样可行:
[[[dict objectForKey:@"key1"] objectForKey:@"@specialKey"] objectForKey:@"key3"]
但是这些都不起作用:
[dict valueForKeyPath:@"[email protected]"]
[dict valueForKeyPath:@"key1.@@specialKey.key3"]
任何想法?
谢谢,
Mike
推荐答案
如果你想使用键值编码,你不应该使用带有你的键名的@符号。
you shouldn't be using @ signs with your key names if you want to use key value coding.
用于键名a重新如下:
apple's guidelines for key names are as follows:
您必须找到一种解决方法,以便在您获得密钥时重新格式化密钥字符串,使其符合KVC标准。
You'll have to find a workaround to reformat the key string whereever you're getting your keys from to be KVC compliant.
这篇关于如果一个键启动@符号,则在NSDictionary上使用valueForKeyPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!