检查 NSDictionary 中是否存在键

发布于 2024-10-11 04:34:59 字数 232 浏览 7 评论 0原文

如何检查该密钥是否存在?:

[[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"]

我想知道该密钥是否存在。我怎样才能做到这一点?

非常感谢:)

编辑: dataArray 中有对象。这些对象就是 NSDictionaries。

how can I check if this exists?:

[[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"]

I want to know whether this key exists or not. How can I do that?

Thank you very much :)

EDIT:
dataArray has Objects in it. And these objects are NSDictionaries.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

掩耳倾听 2024-10-18 04:35:00

检查字典是否包含任何值。我更喜欢 [dic allKeys].count > 0 进行检查。

Check dictionary contains any value. I prefer [dic allKeys].count > 0 to check.

椒妓 2024-10-18 04:35:00

使用 (unsigned long) 选项:

if ( (unsigned long)[[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] ) {
    // Key exist;
}else{
    // Key not exist;
};

Use the (unsigned long) option:

if ( (unsigned long)[[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] ) {
    // Key exist;
}else{
    // Key not exist;
};
陌路黄昏 2024-10-18 04:34:59

我认为 [dataArray objectAtIndex:indexPathSet.row] 返回 NSDictionary,在这种情况下,您可以简单地检查 valueForKeynil 的结果。

例如:

if ([[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) {
    // The key existed...

}
else {
    // No joy...

}

I presume that [dataArray objectAtIndex:indexPathSet.row] is returning an NSDictionary, in which case you can simply check the result of valueForKey against nil.

For example:

if ([[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) {
    // The key existed...

}
else {
    // No joy...

}
宛菡 2024-10-18 04:34:59

所以我知道您已经选择了一个答案,但我发现这作为 NSDictionary 上的一个类别非常有用。此时,您将开始通过所有这些不同的答案来提高效率。嗯... 1 中的 6...

- (BOOL)containsKey: (NSString *)key {
     BOOL retVal = 0;
     NSArray *allKeys = [self allKeys];
     retVal = [allKeys containsObject:key];
     return retVal;
}

So I know you already selected an answer, but I found this to be rather useful as a category on NSDictionary. You start getting into efficiency at this point with all these different answers. Meh...6 of 1...

- (BOOL)containsKey: (NSString *)key {
     BOOL retVal = 0;
     NSArray *allKeys = [self allKeys];
     retVal = [allKeys containsObject:key];
     return retVal;
}
苦行僧 2024-10-18 04:34:59

检查是否为零:

if ([[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) {
    // SetEntries exists in this dict
} else {
    // No SetEntries in this dict
}

Check if it's nil:

if ([[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) {
    // SetEntries exists in this dict
} else {
    // No SetEntries in this dict
}
飞烟轻若梦 2024-10-18 04:34:59

这也适用于使用以下语法的 Objective-C 文字:

NSDictionary *dict = @{ @"key1" : @"value1", @"key2" : @"value2" };
if (dict[@"key2"])
NSLog(@"Exists");
else
NSLog(@"Does not exist");

this also works using Objective-C literals using the following syntax:

NSDictionary *dict = @{ @"key1" : @"value1", @"key2" : @"value2" };
if (dict[@"key2"])
NSLog(@"Exists");
else
NSLog(@"Does not exist");
小女人ら 2024-10-18 04:34:59

试试这个:

if ([dict objectForKey:@"bla"]) {
   // use obj
} else {
   // Do something else like create the object
}

Try this:

if ([dict objectForKey:@"bla"]) {
   // use obj
} else {
   // Do something else like create the object
}
你列表最软的妹 2024-10-18 04:34:59

这个功能与此相同,但代码更少:

if (dataArray[indexPathSet.row][@"SetEntries"] != nil) { /* the key exists */        } 
else                                                   { /* the key doesn't exist */ }

This one does the same but with less code:

if (dataArray[indexPathSet.row][@"SetEntries"] != nil) { /* the key exists */        } 
else                                                   { /* the key doesn't exist */ }
⒈起吃苦の倖褔 2024-10-18 04:34:59
if ((NSNull *)[[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) {
    // SetEntries exists in this dict
} else {
    // No SetEntries in this dict
}

这是正确的答案。

if ((NSNull *)[[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) {
    // SetEntries exists in this dict
} else {
    // No SetEntries in this dict
}

That's the right answer.

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