0

我有一串与他们的位置有关的照片网址(例如: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 循环之外检索核心数据对象时,它给了我这个:-“googleusercontent.com/photo1.gif”-“googleusercontent.com/photo1.gif”

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

4

1 回答 1

0

您的urls变量是 an NSString,您将其归档,然后将其取消归档为 an NSMutableArray,这是不正确的。我认为你想要做的是NSMutableArrayNSStrings你的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]; 

    ...
}
于 2013-10-08T03:43:28.260 回答