Reputation: 761
I am trying to access all of my NSDictionary values after call a method that sets my dictionary items. For some reason after I invoke the method and try to log out all of my items, it only returns one item and it's also the last item in the dictionary.
Here's where I set the dictionary:
-(void)setDestinationDictionary {
destinationFileDict = [[NSMutableDictionary alloc] init];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *DestinationFiles = [fileManager contentsOfDirectoryAtPath:destinationPath error:nil];
for (NSString *file in DestinationFiles) {
NSString *sourceFile = [[destinationPath stringByAppendingPathComponent:file] lastPathComponent];
NSImage *image = [NSImage imageNamed:sourceFile];
CGFloat imageWidth = [image size].width;
CGFloat imageHeight = [image size].height;
[destinationFileDict setObject:sourceFile forKey:@"fileName"];
[destinationFileDict setObject:[NSNumber numberWithFloat:imageWidth] forKey:@"imageWidth"];
[destinationFileDict setObject:[NSNumber numberWithFloat:imageHeight] forKey:@"imageHeight"];
}
Here's where I try to call the method and log the contents of the dictionary:
-(void)initiateCopy {
[self setDestinationDictionary];
NSLog(@"%@", destinationFileDict);
}
There should be a total of 18 entries in the dictionary, but I'm only returning one and it's the last record.
Upvotes: 0
Views: 37
Reputation: 385580
Your dictionary contains three keys: fileName
, imageWidth
, and imageHeight
.
In setDestinationDictionary
, on each pass through the for
loop, you set the same three keys in the same dictionary. Each pass replaces the values you stored on the prior pass. Thus at the end of setDestinationDictionary
, the dictionary still contains only three keys, and the values are the values set in the last pass through the loop.
Perhaps you want to create an array of dictionaries, one for each destination file. Or perhaps you want to use the filename as the key and store the size as the value (in which case, look at +[NSValue valueWithSize:]
and -[NSValue sizeValue]
).
Upvotes: 3