Can not get all associated strings from NSDictionary iOS

advertisements

I am new to iOS and little weak in logic.

Can any one help me here,

I want to get all matching data from NSDictionary.

eg:

my tempDict is NSDictionary which contains this,

tempDict:

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;
    }
);
}

Inside a pastConsultations key i want to get all nested data which contains AM

i.e my output should be:

{ //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;
        }
}

Here is the code i have tried but it is not working, please help where i am making mistake?

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;
                }
            }
        }


Try this code:

assuming you already have tempDict dictionary loaded.

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.

EDIT: If you want to combine search results, for example, results for AM entry and Friday only you can do like this:

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];
  }
}