这是我的代码,我目前如何从Dictionary数组中访问该数据,但我知道有简单的优化代码可以做到这一点。我是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";
},
)
答案 0 :(得分:0)
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;
}
}
答案 1 :(得分:0)
您可以使用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 );