gpt4 book ai didi

ios - 无法从 NSDictionary iOS 获取所有匹配字符串

转载 作者:行者123 更新时间:2023-11-29 00:33:25 27 4
gpt4 key购买 nike

我是 iOS 新手,逻辑有点薄弱。

谁能帮帮我,

我想从 NSDictionary 获取所有匹配的数据。

例如:

我的tempDict是包含这个的NSDictionary,

临时字典:

errorCode = 00;
errorMessage = "<null>";
pastConsultations = (

{
date = "16 December 2016";
day = Friday;
doctorName = "<null>";
localTime = "07:30 AM";
symptoms = q;
today = Yes;
},
{
date = "16 December 2016";
day = Friday;
doctorName = "<null>";
localTime = "07:30 AM";
symptoms = g;
today = Yes;
},
{
date = "13 December 2016";
day = Tuesday;
doctorName = "<null>";
localTime = "12:30 PM";
symptoms = fever;
today = No;
}
);
upcomingConsultations = (
{
date = "16 December 2016";
day = Friday;
localTime = "09:30 PM";
symptoms = "";
today = Yes;
},
{
date = "16 December 2016";
day = Friday;
localTime = "09:30 PM";
symptoms = chj;
today = Yes;
},
{
date = "18 December 2016";
day = Sunday;
localTime = "12:30 PM";
symptoms = "test an incoming ";
today = No;
}
);
}

pastConsultations键内,我想获取包含AM的所有嵌套数据

即我的输出应该是:

{ //Array at 0th index
date = "16 December 2016";
day = Friday;
doctorName = "<null>";
localTime = "07:30 AM";
symptoms = q;
today = Yes;
},
{ //Array at 1st index
date = "16 December 2016";
day = Friday;
doctorName = "<null>";
localTime = "07:30 AM";
symptoms = g;
today = Yes;
}
}

这是我尝试过的代码,但它不起作用,请帮助我哪里出错了?

NSArray *allKeys;
for (int i=0; i<[tempDict count]; i++) {
allKeys = [[[tempDict valueForKey:@"upcomingConsultations"] objectAtIndex:i] allKeys];



NSString *targetKey = nil;
// NSArray *allKeys = [[tempDict valueForKeyPath:@"pastConsultations"] allKeys];
for (int j = 0; j < [allKeys count]; ++j) {
NSString *key = [allKeys objectAtIndex:i];
NSString *obj = [[[tempDict valueForKey:@"upcomingConsultations"] objectAtIndex:i] objectForKey:key];
if ([obj rangeOfString:searchText].location != NSNotFound) { // searchedString is what you're looking for
targetKey = key;
NSLog(@"found match");
break;
}
}
}

最佳答案

试试这段代码:

假设您已经加载了 tempDict 字典。

NSMutableArray *morningPastArr = [[NSMutableArray alloc] init];

NSArray *pastArray = [tempDict objectForKey:@"pastConsultations"];
for (int i=0;i<[pastArray count]; i++) {
NSDictionary *eachPast = [pastArray objectAtIndex:i];
NSString *time = [eachPast objectForKey:@"localTime"];
if (![time rangeOfString:@"AM"].location == NSNotFound) {
[morningPastArr addObject:eachPast];
}
}

// finally you will have like what you need in morningPastArr.

编辑:如果您想合并搜索结果,例如上午条目和仅限星期五的结果,您可以这样做:

NSMutableArray *morningAndFridayPastArr = [[NSMutableArray alloc] init];

NSArray *pastArray = [tempDict objectForKey:@"pastConsultations"];
for (int i=0;i<[pastArray count]; i++) {
NSDictionary *eachPast = [pastArray objectAtIndex:i];
NSString *time = [eachPast objectForKey:@"localTime"];
NSString *day = [eachPast objectForKey:@"day"];
if ((![time rangeOfString:@"AM"].location == NSNotFound)&&([day isEqualToString:@"Friday"])) {
[morningAndFridayPastArr addObject:eachPast];
}
}

关于ios - 无法从 NSDictionary iOS 获取所有匹配字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41180182/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com