Saaggar Saaggar - 4 months ago 8
iOS Question

get value from dict

I have following json response.

{
bufferTime = 15;
cutOffTime = "02:00";
shiftId = 1;
shiftTime = "07:00";
tripType = PICKUP;
},
{
bufferTime = 15;
cutOffTime = "02:00";
shiftId = 2;
shiftTime = "09:00";
tripType = PICKUP;
},


And i use the following to store the response in NSMutable arrays.

{
for (int i = 0; i < numberOfItems; i++) {
NSDictionary *dict = [res objectAtIndex:i];
[shiftIdArray addObject: [dict objectForKey:@"shiftId"]];
[shiftTimeArray addObject: [dict objectForKey:@"shiftTime"]];
[cutOffTimeArray addObject:[dict objectForKey:@"cutOffTime"]];
//NSLog(@"dict : %@",dict);
}
self.dpShiftTimePicker = [[DownPicker alloc] initWithTextField:self.txtShiftTime withData:shiftTimeArray];
[self.dpShiftTimePicker addTarget:self
action:@selector(dp_Selected2:)
forControlEvents:UIControlEventValueChanged];
}

-(void)dp_Selected2:(id)dp{
NSString* selectedValue = [self.dpShiftTimePicker text];
NSLog(@"selectedValue: %@",selectedValue);
}


I pass the shiftTimeArray in my picker which I made programatically. Now, upon selecting a particular shiftTime for e.g 09:00 i want to get it's corresponding shiftId and cutOffTime and store in a string. Kindly, help.

Answer

try this -

-(void)dp_Selected2:(id)dp{

  NSString* selectedValue = [self.dpShiftTimePicker text];

  NSString* selectedIDValue   [shiftIdArray objectAtIndex:[shiftTimeArray indexOfObject:selectedValue]];

  NSString* selectedCutOFFValue   [cutOffTimeArray objectAtIndex:[shiftTimeArray indexOfObject:selectedValue]];

  NSLog(@"selectedValue: %@",selectedValue);
 }