Gulfer Gulfer - 5 months ago 15
iOS Question

Find Dictionary Key inside and Array of Dictionaries

I have an array that holds several

NSMutableDictionary
objects.

Each time text fields are filled out and the submit button is pressed:

//The strings
NSString *dateString = @"Date";
NSString *timeString = @"Time";
NSString *typeString = @"Type";

- (IBAction)addData:(id)sender {

[_myDictionary setObject:_dateLabel.text forKey:dateString];
[_myDictionary setObject:_nNumber.text forKey:timeString];
[_myDictionary setObject:_type.text forKey:typeString];

[_myArray addObject:_myDictionary];

[self display]
}


Then I am trying to display the results in a UItextView (in this example the date) but I don't know how.

-(void)displayData{

for (int i = 0; i<_myArray.count; i++) {
[_myArray objectAtIndex:i];
outputString = [outputString stringByAppendingFormat:@"%@\n",[_myDictionary objectForKey:@"Date"]];

}
_myTextView.text = [NSString stringWithFormat:@"%@",outputString];

}


This ends up displaying the date for the last Dictionary in the array. I can't even figure out how to log the date from a specific dictionary.

Say
[_myArray objectAtIndex:3] [_myDictionary objectForKey:@"Date"]
. How would I log the date from the 3rd index?

Hope this makes sense

EDIT SOLUTION

for (int i = 0; i<_myArray.count; i++)
{

outputString = [outputString stringByAppendingFormat:@"%@ %@\n",[[_myArray objectAtIndex:i]objectForKey:@"Date"], [[_myArray objectAtIndex:i]objectForKey:@"Time"]];

}
_myTextView.text = [NSString stringWithFormat:@"%@",outputString];

}


Thanks for all the quick help!!

Answer
for(int i =0 ;i <[array count];i++)
{
  NSLog(@"%@",[[array objectAtIndex:i]allKeys]);
 }

this method will print all the keys

Comments