如何处理 '[<__nscfstring 0x2f1730>valueForUndefinedKey:]: 此类对于键 $oid' 来说不符合键值编码。错误
我遇到了错误(主题中所述),因为有时属性“id”不会在返回的 json 中存储包含“$oid”的哈希。例如
,有时我得到:
"id":{"$oid":"4eea972209f47a0028000140"}
有时我得到
"id":"4eea972209f47a0028000140"
我试图在以下代码中进行检查以适应这种不规则性
if ([[question valueForKey:@"id"] valueForKey:@"$oid"])
{
question_id = [[question valueForKey:@"id"] valueForKey:@"$oid"];
}
else
{
question_id = [question valueForKey:@"id"];
}
但是,它仍然不起作用,因为代码在检查阶段失败。
如何实施检查,以便仅在“$oid”存在时才从“$oid”中获取 Question_id?
I am hitting the error (stated in the subject) because there are times the property 'id' does not store the hash containing '$oid' in the returned json. For example
Sometimes I get:
"id":{"$oid":"4eea972209f47a0028000140"}
Some other times I get
"id":"4eea972209f47a0028000140"
I am trying to do a check in the following code to cater for such irregularity
if ([[question valueForKey:@"id"] valueForKey:@"$oid"])
{
question_id = [[question valueForKey:@"id"] valueForKey:@"$oid"];
}
else
{
question_id = [question valueForKey:@"id"];
}
However, it still doesn't work as the code fails during the checking phase.
How can I implement a check so that I will take question_id from '$oid' only if it exists?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试以下代码。
Try the following code.
您需要检查
[question valueForKey:@"id"]
返回的类型,有时它看起来是一个字符串,有时它是另一个与您的其他密钥兼容的 KVC 对象。您的错误将出现在第一个 if 语句中。You need to check what type is being returned by
[question valueForKey:@"id"]
, it looks like sometimes it is a string, and sometimes it is another object which is KVC compliant for your other key. Your error will be in the very first if statement.