Almost a solution to the Challenge


#1

Can anyone tell me why it’s only printing the last thing added to the array?

[code]//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

@autoreleasepool {
    
    NSMutableArray *propertyList = [[NSMutableArray alloc] init];
    
    NSMutableDictionary *oneType;
    
    oneType = [NSMutableDictionary dictionary];
    [oneType setObject:@"array"
                forKey:@"type1"];
    [propertyList addObject:oneType];
    
    [oneType setObject:@"dictionary"
                forKey:@"type1"];
    [propertyList addObject:oneType];
    
    [oneType setObject:@"string"
                forKey:@"type1"];
    [propertyList addObject:oneType];
    
    [oneType setObject:@"data"
                forKey:@"type1"];
    [propertyList addObject:oneType];
    
    [oneType setObject:@"date"
                forKey:@"type1"];
    [propertyList addObject:oneType];
    
    [oneType setObject:@"integer"
                forKey:@"type1"];
    [propertyList addObject:oneType];
    
    [oneType setObject:@"float"
                forKey:@"type1"];
    [propertyList addObject:oneType];
    
    [oneType setObject:@"boolean"
                forKey:@"type1"];
    [propertyList addObject:oneType];
    
    [propertyList writeToFile:@"/tmp/ch26challenge.plist"
                   atomically:YES];
    
    NSArray *ch26Challenge = [NSArray arrayWithContentsOfFile:@"/tmp/ch26challenge.plist"];
    
    for (NSDictionary *d in ch26Challenge){
        NSLog(@"There are 8 types of data for this list, one is %@.", [d objectForKey:@"type1"]);
    }
    
}
return 0;

}

[/code]

it’s printing out this:
2012-02-29 11:46:35.221 Ch 28 Challenge Property Lists[348:707] There are 8 types of data for this list, one is boolean.
2012-02-29 11:46:35.223 Ch 28 Challenge Property Lists[348:707] There are 8 types of data for this list, one is boolean.
2012-02-29 11:46:35.224 Ch 28 Challenge Property Lists[348:707] There are 8 types of data for this list, one is boolean.
2012-02-29 11:46:35.224 Ch 28 Challenge Property Lists[348:707] There are 8 types of data for this list, one is boolean.
2012-02-29 11:46:35.225 Ch 28 Challenge Property Lists[348:707] There are 8 types of data for this list, one is boolean.
2012-02-29 11:46:35.226 Ch 28 Challenge Property Lists[348:707] There are 8 types of data for this list, one is boolean.
2012-02-29 11:46:35.226 Ch 28 Challenge Property Lists[348:707] There are 8 types of data for this list, one is boolean.
2012-02-29 11:46:35.227 Ch 28 Challenge Property Lists[348:707] There are 8 types of data for this list, one is boolean.

thanks


#2

So, you created an array and a dictionary. You put the same dictionary in the array 8 times.

And on that dictionary you called setObject:forKey: with the same key over and over. Because a key can only appear once, each time you called setObject:forKey:, you replaced the key-value pair you had put in on the previous call.

Look over some other people’s solutions.


#3

Thanks again…