不使用 NSPredicate 我想从字典数组中获取数据
Without using NSPredicate i want to get data out from array of dictioary
这是我的代码,我目前如何从字典数组访问该数据,但我知道有简单和优化的代码可以做到这一点。我是 ios
的新手
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"description LIKE[cd] 'CallCenterPhoneNumber' "];
NSArray *PhoneNumberArrary = [tblData filteredArrayUsingPredicate:predicate];
NSDictionary *item = [PhoneNumberArrary objectAtIndex:0];
phoneNumber=[item objectForKey:@"value"];
NSLog(@"%@",phoneNumber);
predicate = [NSPredicate predicateWithFormat:@"description LIKE[cd] 'CallCenterEmailAddress' "];
NSArray *EmailArrary = [tblData filteredArrayUsingPredicate:predicate];
NSDictionary *item1 = [EmailArrary objectAtIndex:0];
EmailAddress=[item1 objectForKey:@"value"];
NSLog(@"%@",EmailAddress);
//and this is my array of Dictionary.
(
{
description = CallCenterPhoneNumber;
id = 0;
value = 123;
},
{
description = CallCenterEmailAddress;
id = 1;
value = "email@domain.com";
},
)
for (NSDictionary *dictionary in tblData) {
if ([dictionary.description rangeOfString:@"CallCenterPhoneNumber" options:NSCaseInsensitiveSearch].location != NSNotFound) {
phoneNumber = dictionary;
} else if ([dictionary.description rangeOfString:@"CallCenterEmailAddress" options:NSCaseInsensitiveSearch].location != NSNotFound) {
emailAddress = dictionary;
}
}
您可以使用 for ( ... in ... )
循环遍历数组的每个条目,并使用简化语法从每个字典中提取信息。例如,如果 entry
是一个指向字典的指针,那么
entry[@"description"];
等同于
[entry objectForKey:@"description"];
id phoneNumber = nil;
id emailAddress = nil;
for ( NSDictionary *entry in array )
{
NSString *description = entry[@"description"];
if ( [description isEqualToString:@"CallCenterPhoneNumber"] )
phoneNumber = entry[@"value"];
else if ( [description isEqualToString:@"CallCenterEmailAddress"] )
emailAddress = entry[@"value"];
}
NSLog( @"%@", phoneNumber );
NSLog( @"%@", emailAddress );
这是我的代码,我目前如何从字典数组访问该数据,但我知道有简单和优化的代码可以做到这一点。我是 ios
的新手NSPredicate *predicate = [NSPredicate predicateWithFormat:@"description LIKE[cd] 'CallCenterPhoneNumber' "];
NSArray *PhoneNumberArrary = [tblData filteredArrayUsingPredicate:predicate];
NSDictionary *item = [PhoneNumberArrary objectAtIndex:0];
phoneNumber=[item objectForKey:@"value"];
NSLog(@"%@",phoneNumber);
predicate = [NSPredicate predicateWithFormat:@"description LIKE[cd] 'CallCenterEmailAddress' "];
NSArray *EmailArrary = [tblData filteredArrayUsingPredicate:predicate];
NSDictionary *item1 = [EmailArrary objectAtIndex:0];
EmailAddress=[item1 objectForKey:@"value"];
NSLog(@"%@",EmailAddress);
//and this is my array of Dictionary.
(
{
description = CallCenterPhoneNumber;
id = 0;
value = 123;
},
{
description = CallCenterEmailAddress;
id = 1;
value = "email@domain.com";
},
)
for (NSDictionary *dictionary in tblData) {
if ([dictionary.description rangeOfString:@"CallCenterPhoneNumber" options:NSCaseInsensitiveSearch].location != NSNotFound) {
phoneNumber = dictionary;
} else if ([dictionary.description rangeOfString:@"CallCenterEmailAddress" options:NSCaseInsensitiveSearch].location != NSNotFound) {
emailAddress = dictionary;
}
}
您可以使用 for ( ... in ... )
循环遍历数组的每个条目,并使用简化语法从每个字典中提取信息。例如,如果 entry
是一个指向字典的指针,那么
entry[@"description"];
等同于
[entry objectForKey:@"description"];
id phoneNumber = nil;
id emailAddress = nil;
for ( NSDictionary *entry in array )
{
NSString *description = entry[@"description"];
if ( [description isEqualToString:@"CallCenterPhoneNumber"] )
phoneNumber = entry[@"value"];
else if ( [description isEqualToString:@"CallCenterEmailAddress"] )
emailAddress = entry[@"value"];
}
NSLog( @"%@", phoneNumber );
NSLog( @"%@", emailAddress );