NSMutuableArray&核心数据表现不正常

时间:2013-10-08 00:17:04

标签: ios objective-c

我有一系列照片网址,与他们的位置有关(e.x。:googleusercontent.com/photo.gif)。我使用'二进制数据'类型将单个字符串(字符串可能包含四个其他URL)保存到Core Data。当我从核心数据中检索URL时,它会正确显示字符串的数量,但不会在for循环之外显示正确的数据。

// Loop through the photo selection to get the urls
for (int i=0; i < self.tempPhotos.count; i++)
{
    self.photo =  [ self.tempPhotos objectAtIndex:i]; 
    self.image =  [ photo originalImage]; 
    NSString *urls = [image.URL absoluteString]; // <-- here we get the urls and store
    self.selected_urls = urls; 

    NSData * data = [NSKeyedArchiver archivedDataWithRootObject:urls]; 
    self.group.selectedurl = data;   // assign it to the core data object

}

// Now we're out of the for loop, here is where it will not retrieve and log properly. if i put this inside the for loop, it will. why is that? 
NSMutableArray *temp = [NSMutableArray *)[NSKeyedUnarchiver   unarchiveObjectWithData:self.group.selectedurl];
NSLog(@"%@",temp); 

如果我把NSLog放在for循环中,我就能正确理解。它应该看起来像这样: - “googleusercontent.com/photo1.gif” - “googleusercontent.com/photo2.gif”

当我在for循环之外检索Core Data对象时,它给了我: - “googleusercontent.com/photo1.gif” - “googleusercontent.com/photo1.gif”

我不知道为什么它在for循环中工作,但它在外面的任何地方都无法正常工作,我觉得我错过了一个明显的步骤。我可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您的urls变量是NSString,您将其归档,然后以NSMutableArray取消归档,这是不正确的。我认为您尝试做的是在NSMutableArray循环中构建NSStrings for

NSMutableArray *urls = [NSMutableArray arrayWithCapacity:self.tempPhotos.count];
for (int i=0; i < self.tempPhotos.count; i++)
{
    ...

    [urls addObject:[image.URL absoluteString]];

    ...

    NSData * data = [NSKeyedArchiver archivedDataWithRootObject:urls]; 

    ...
}