Reputation: 4732
I have an NSArray
of NSDictionaries
each of which has 4 key values.
I'm creating objects for each NSDictionary and assigning the keys accordingly.
How can I iterate through the array of dictionaries and set each key as an attribute for the object?
I created the array seen in the picture below with this code:
if (muscleArray == nil)
{
NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.muscleArray = rootLevel;
}
NSMutableArray *arrayForSearching = [NSMutableArray array];
for (NSDictionary *muscleDict in self.muscleArray)
for (NSDictionary *excerciseDict in [muscleDict objectForKey:@"exercises"])
[arrayForSearching addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[excerciseDict objectForKey:@"exerciseName"], @"exerciseName",
[muscleDict objectForKey:@"muscleName"], @"muscleName",
[muscleDict objectForKey:@"musclePicture"], @"musclePicture", nil]];
self.exerciseArray = arrayForSearching;
NSString *path = [[NSBundle mainBundle] pathForResource:@"ExerciseDescriptions"
ofType:@"plist"];
NSDictionary *descriptions = [NSDictionary dictionaryWithContentsOfFile:path];
NSMutableArray *exercises = self.exerciseArray;
for (NSInteger i = 0; i < [exercises count]; i++) {
NSDictionary *dict = [[exercises objectAtIndex:i] mutableCopy];
NSString *exerciseName = [dict valueForKey:@"exerciseName"];
NSString *description = [descriptions valueForKey:exerciseName];
[dict setValue:description forKey:@"exerciseDescription"];
[exercises replaceObjectAtIndex:i withObject:dict];
}
The code to create one object would look like this:
PFObject *preloadedExercises = [[PFObject alloc] initWithClassName:@"preloadedExercises"];
[preloadedExercises setObject:exerciseName forKey:@"exerciseName"];
[preloadedExercises saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
NSLog(@"Success");
} else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
The array of dictionaries looks like this:
Upvotes: 0
Views: 5629
Reputation: 30846
// Assuming you want to do something with all of these objects you're creating
// We'll start by creating an NSMutableArray
NSMutableArray *newObjects = [NSMutableArray arrayWithCapacity:arrayOfDictionaries.count];
for (NSDictionary *dictionary in arrayOfDictionaries)
{
PFObject *object = [PFObject objectWithClassName:@"preloadedExercises"];
object.exerciseDescription = [dictionary objectForKey:@"exerciseDescription"];
object.exerciseName = [dictionary objectForKey:@"exerciseName"];
object.muscleName = [dictionary objectForKey:@"muscleName"];
object.musclePicture = [dictionary objectForKey:@"musclePicture"];
// Add object to mutable array
[newObjects addObject:object];
}
Upvotes: 3
Reputation: 3103
After a quick glance at the Parse SDK that you mentioned in the comments, I think what you are looking for is this:
NSMutableArray *exercisesArray = [[NSMutableArray alloc] init];
PFObject *preloadedExercises;
id value;
// Iterate through your array of dictionaries
for (NSDictionary *muscleDict in self.muscleArray) {
// Create our object
preloadedExercises = [PFObject objectWithClassName:@"preloadedExercises"];
// For each dictionary, iterate through its keys
for (id key in muscleDict) {
// Grab the value
value = [muscleDict objectForKey:key];
// And assign each attribute of the object to the corresponding values
[preloadedExercises setObject:value forKey:key];
}
// Finally, add this newly created object to your array
[exercisesArray addObject: preloadedExercises];
}
Upvotes: 2
Reputation: 12405
for(NSDictionary* dictionary in yourArray){
// here you can iterate through. and assign every dictionary as you wish to.
}
Upvotes: 0