如何处理 '[<__nscfstring 0x2f1730>valueForUndefinedKey:]: 此类对于键 $oid' 来说不符合键值编码。错误

发布于 2024-12-24 23:02:16 字数 509 浏览 2 评论 0原文

我遇到了错误(主题中所述),因为有时属性“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尛丟丟 2024-12-31 23:02:16

尝试以下代码。

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

Try the following code.

id quesDict = [question valueForKey:@"id"];
if( [quesDict isKindOfClass:[NSDictionary class]] )
{
    question_id = [quesDict valueForKey:@"$oid"];
}
else
{
    question_id = quesDict;
}
半枫 2024-12-31 23:02:16

您需要检查 [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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文