我正在编写的for循环一直覆盖自己,我需要能够返回所有的值。如何防止for循环覆盖其自身?
sourceArray = [[NSMutableArray alloc] init];
for (NSString *file in sourceFiles) {
NSString *sourceFile = [sourcePath stringByAppendingPathComponent:file];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:sourceFile];
NSImageRep *rep = [[image representations] objectAtIndex:0];
sourceArray = [NSMutableArray arrayWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:
sourceFile.lastPathComponent, @"fileName",
[NSNumber numberWithLong:rep.pixelsWide], @"width",
[NSNumber numberWithLong:rep.pixelsHigh], @"height",
nil], nil];
}发布于 2015-08-20 20:45:48
问题是在每次遍历循环时,[NSMutableArray arrayWithObjects:都会替换数组。而是..。
// prettier using modern clang literals...
NSDictionary *d = @{ @"fileName": sourceFile.lastPathComponent,
@"width": @(rep.pixelsWide),
@"height": @(rep.pixelsHeight) };
// just add to the array we allocated outside the loop
[sourceArray addObject:[d mutableCopy]];https://stackoverflow.com/questions/32127987
复制相似问题