coc*_*ner 75 iphone objective-c nsdictionary ios
我如何检查是否存在?:
[[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"]
Run Code Online (Sandbox Code Playgroud)
我想知道这个密钥是否存在.我怎样才能做到这一点?
非常感谢你 :)
编辑:dataArray中有对象.这些对象是NSDictionaries.
Joh*_*ker 157
我认为这[dataArray objectAtIndex:indexPathSet.row]是返回一个NSDictionary,在这种情况下你可以简单地检查valueForKey反对的结果nil.
例如:
if ([[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) {
// The key existed...
}
else {
// No joy...
}
Run Code Online (Sandbox Code Playgroud)
Mil*_*den 46
所以我知道你已经选择了答案,但我发现这个作为一个类别非常有用NSDictionary.在这一点上,您开始通过所有这些不同的答案开始提高效率.嗯... 6 of 1 ...
- (BOOL)containsKey: (NSString *)key {
BOOL retVal = 0;
NSArray *allKeys = [self allKeys];
retVal = [allKeys containsObject:key];
return retVal;
}
Run Code Online (Sandbox Code Playgroud)
Bol*_*ock 34
检查它是否为零:
if ([[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) {
// SetEntries exists in this dict
} else {
// No SetEntries in this dict
}
Run Code Online (Sandbox Code Playgroud)
这也可以使用以下语法使用Objective-C文字:
NSDictionary *dict = @{ @"key1" : @"value1", @"key2" : @"value2" };
if (dict[@"key2"])
NSLog(@"Exists");
else
NSLog(@"Does not exist");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
91434 次 |
| 最近记录: |