如何处理 '[<__NSCFString 0x2f1730>valueForUndefinedKey:]: 此类对于键 $oid' 错误不符合键值编码 [英] How to handle '[<__NSCFString 0x2f1730> valueForUndefinedKey:]: this class is not key value coding-compliant for the key $oid' error

查看:154
本文介绍了如何处理 '[<__NSCFString 0x2f1730>valueForUndefinedKey:]: 此类对于键 $oid' 错误不符合键值编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了错误(在主题中说明),因为有时属性id"不会在返回的 json 中存储包含$oid"的哈希值.例如

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

有时我会:

"id":{"$oid":"4eea972209f47a0028000140"}

"id":{"$oid":"4eea972209f47a0028000140"}

有时我会得到

"id":"4eea972209f47a0028000140"

"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.

如何进行检查,以便仅在存在 '$oid' 时才从 question_id 中获取它?

How can I implement a check so that I will take question_id from '$oid' only if it exists?

推荐答案

试试下面的代码.

id quesDict = [question valueForKey:@"id"];
if( [quesDict isKindOfClass:[NSDictionary class]] )
{
    question_id = [quesDict valueForKey:@"$oid"];
}
else
{
    question_id = quesDict;
}

这篇关于如何处理 '[<__NSCFString 0x2f1730>valueForUndefinedKey:]: 此类对于键 $oid' 错误不符合键值编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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