如果一个键启动@符号,则在NSDictionary上使用valueForKeyPath? [英] Using valueForKeyPath on NSDictionary if a key starts the @ symbol?

查看:123
本文介绍了如果一个键启动@符号,则在NSDictionary上使用valueForKeyPath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 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:@"key1.@specialKey.key3"]
[dict valueForKeyPath:@"key1.@@specialKey.key3"]

任何想法?

谢谢,

Mike

推荐答案

如果你想使用键值编码,你不应该使用带有你的键名的@符号。

you shouldn't be using @ signs with your key names if you want to use key value coding.

apple的指南用于键名a重新如下:

apple's guidelines for key names are as follows:


密钥必须使用ASCII编码,以小写字母开头
,并且可能不包含
空白。

Keys must use ASCII encoding, begin with a lowercase letter, and may not contain whitespace.

您必须找到一种解决方法,以便在您获得密钥时重新格式化密钥字符串,使其符合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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆