gpt4 book ai didi

ios - valueForKey 返回零

转载 作者:行者123 更新时间:2023-12-01 15:56:26 24 4
gpt4 key购买 nike

愚蠢的问题警告......我可能会被剥夺 sleep ,但我似乎无法弄清楚这一点。

'current' 是一个 NSDictionary

if ([current valueForKey:@"name"] != nil)
{
NSLog(@"here %@", [current valueForKey:@"name"]);
}

这打印

here John
here Sam
here <null>
here <null>
here Billy

'if xx != nil' 不应该阻止空值吗?我的字典里有'name',但是name的值可能为空。

最佳答案

NSArrayNSDictionary 不能包含 nil 值。 NSNULL 是专门作为 nil 的占位符创建的。可以放入集合类,只占用空间。

NSNull 定义了一个单例对象,这意味着永远只有一个 NSNull 实例,但它可以用在任意多的地方。

你需要像这样检查....

if (current != nil || [current valueForKey:@"name"] != [NSNull null] )
{
NSLog(@"here %@", [current valueForKey:@"name"]);
}

我相信你会得到这个输出.....

输出

here John
here Sam
here Billy

关于ios - valueForKey 返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17568522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com