2014-02-07 60 views
0

我有兩種方法。從第一種方法我發送數組到第二種方法。在我的第一個方法中,[數組數]值是2.但是在seconf方法中,值是1.但是它們在兩種方法中都應該是相同的。我知道這是一個愚蠢的錯誤。但我不完全明白我犯了錯誤的地方。陣列計數錯誤

第一種方法:

-(void)uploadOverlayManually: (NSMutableArray *)path{ 

NSFileManager *fileManager = [NSFileManager defaultManager]; 

NSLog(@"Array count #1: %d",[path count]); 

for (int i =0; i < [path count]; i++) { 

    imagePath = [path objectAtIndex:i]; 

    NSString *infoPath = [[imagePath stringByDeletingPathExtension] stringByAppendingPathExtension:@"info"]; 
    NSData *infoData = [[NSMutableData alloc] initWithContentsOfFile:infoPath]; 

    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:infoData]; 
    MediaInformation *currentInfo = [unarchiver decodeObjectForKey:@"info"]; 
    [unarchiver finishDecoding]; 

    UIImage *baseImage = [UIImage imageWithContentsOfFile:imagePath]; 
    NSData *data = [root addImageOverlay:baseImage withInfo:currentInfo andPath:imagePath]; 

    [data writeToFile:imagePath atomically:YES]; 

    fileUpload = [[NSMutableArray alloc] init]; 

    [fileUpload addObject:imagePath]; 

} 

[self upload:fileUpload]; 

}

第二種方法:

-(void)upload:(NSArray*)filePaths{ 

if (![[DBSession sharedSession] isLinked]) { 

    [[DBSession sharedSession] linkFromController:root]; //root 
} 

NSLog(@"Array count #2: %d",[filePaths count]); 

} 

回答

1

此行

fileUpload = [[NSMutableArray alloc] init]; 

內部你的循環重新創建該陣列。

它應該在循環之前,以便在每次迭代中添加一個對象。

0

您實例上的每個迴路一個新的數組,因此它可以永遠只是1

達到預期的行爲,使用(移動實例圈外):

-(void)uploadOverlayManually: (NSMutableArray *)path{ 

NSFileManager *fileManager = [NSFileManager defaultManager]; 

NSLog(@"Array count #1: %d",[path count]); 

fileUpload = [[NSMutableArray alloc] init]; 

for (int i =0; i < [path count]; i++) { 

    imagePath = [path objectAtIndex:i]; 

    NSString *infoPath = [[imagePath stringByDeletingPathExtension] stringByAppendingPathExtension:@"info"]; 
    NSData *infoData = [[NSMutableData alloc] initWithContentsOfFile:infoPath]; 

    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:infoData]; 
    MediaInformation *currentInfo = [unarchiver decodeObjectForKey:@"info"]; 
    [unarchiver finishDecoding]; 

    UIImage *baseImage = [UIImage imageWithContentsOfFile:imagePath]; 
    NSData *data = [root addImageOverlay:baseImage withInfo:currentInfo andPath:imagePath]; 

    [data writeToFile:imagePath atomically:YES]; 

    [fileUpload addObject:imagePath]; 

} 

[self upload:fileUpload]; 
0

你在裏面分配陣列一個循環請分配它在viewDidLoad或任何函數只調用一次。