如何检查 nsdictionary 键是否有值?
How to check if a nsdictionary key has values or not?
我正在使用以下代码
for(NSDictionary* key in [Tags valueForKey:@"data"]) {
if ([[key valueForKey:@"hashtag"] rangeOfString:self.searchKey options:NSCaseInsensitiveSearch].location != NSNotFound) {
NSLog(@"found key");
}
break;
}
对于 nsdictionary
我不想使用 for 循环来检查第一个键值是否存在。实际上我只是想检查字典中是否返回了主题标签,或者只使用 if 语句。这怎么可能?
您可以使用谓词搜索带主题标签的标签:
// MARK: - Without Loop
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"hashtag != NULL"];
NSArray *hashtaggedTags = [tags filteredArrayUsingPredicate:predicate];
if (hashtaggedTags.count == 0) {
NSLog(@"No hashtags at all");
} else {
NSLog(@"Found some hashtags");
}
正在清理您的代码以获取最小可用代码:
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Assuming you have some dictionary like this
NSDictionary * Tags = @{
@"data": @[ // <- Here is the array
@{ // <- here is the dictionary
@"hashtag": @"NightOut",
@"count" : @0,
}
]
};
// Getting the tag value
NSArray* tags = Tags[@"data"];
// MARK: - Without Loop
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"hashtag != NULL"];
NSArray *hashtaggedTags = [tags filteredArrayUsingPredicate:predicate];
if (hashtaggedTags.count == 0) {
NSLog(@"No hashtags at all");
} else {
NSLog(@"Found some hashtags");
}
// MARK: - With loop
// Loop through tags (not keys of the dictionary)
for (NSDictionary * tag in tags) {
// Check if `hashtag` is exists and returns something in the dictionary or not
if (tag[@"hashtag"]) {
NSLog(@"There's an object set for key @\"hashtag\"!");
} else {
NSLog(@"No object set for key @\"hashtag\"");
}
}
}
return 0;
}
我正在使用以下代码
for(NSDictionary* key in [Tags valueForKey:@"data"]) {
if ([[key valueForKey:@"hashtag"] rangeOfString:self.searchKey options:NSCaseInsensitiveSearch].location != NSNotFound) {
NSLog(@"found key");
}
break;
}
对于 nsdictionary
我不想使用 for 循环来检查第一个键值是否存在。实际上我只是想检查字典中是否返回了主题标签,或者只使用 if 语句。这怎么可能?
您可以使用谓词搜索带主题标签的标签:
// MARK: - Without Loop
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"hashtag != NULL"];
NSArray *hashtaggedTags = [tags filteredArrayUsingPredicate:predicate];
if (hashtaggedTags.count == 0) {
NSLog(@"No hashtags at all");
} else {
NSLog(@"Found some hashtags");
}
正在清理您的代码以获取最小可用代码:
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Assuming you have some dictionary like this
NSDictionary * Tags = @{
@"data": @[ // <- Here is the array
@{ // <- here is the dictionary
@"hashtag": @"NightOut",
@"count" : @0,
}
]
};
// Getting the tag value
NSArray* tags = Tags[@"data"];
// MARK: - Without Loop
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"hashtag != NULL"];
NSArray *hashtaggedTags = [tags filteredArrayUsingPredicate:predicate];
if (hashtaggedTags.count == 0) {
NSLog(@"No hashtags at all");
} else {
NSLog(@"Found some hashtags");
}
// MARK: - With loop
// Loop through tags (not keys of the dictionary)
for (NSDictionary * tag in tags) {
// Check if `hashtag` is exists and returns something in the dictionary or not
if (tag[@"hashtag"]) {
NSLog(@"There's an object set for key @\"hashtag\"!");
} else {
NSLog(@"No object set for key @\"hashtag\"");
}
}
}
return 0;
}