如何遍历包含NSDictionaries的NSArray?

时间:2011-12-09 08:54:53

标签: iphone objective-c cocoa-touch nsarray nsobject

我有一个NSArray NSDictionaries,其中每个都有4个键值。

我正在为每个NSDictionary创建对象并相应地分配密钥。

如何遍历字典数组并将每个键设置为对象的属性?

我使用以下代码创建了下图所示的数组:

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

创建一个对象的代码如下所示:

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

字典数组如下所示:enter image description here

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

在快速浏览一下您在评论中提到的Parse SDK之后,我认为您正在寻找的是:

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

答案 2 :(得分:0)

for(NSDictionary* dictionary in yourArray){
// here you can iterate through. and assign every dictionary as you wish to.
}