2

我正在开发我的第一个 iphone 'Diary' 应用程序,它使用包含 NSString 标题、NSString 文本和 NSDate creationDate 的自定义 'Entry' 对象。当我尝试归档一个 NSMutableArray 的 Entry 对象,然后在下次加载视图时检索它们时,应用程序崩溃了。我已经浏览了一堆使用 NSKeyedArchivers 的示例代码和示例,但仍然无法弄清楚为什么会发生这种情况。我猜保存条目的数组的初始化存在问题,但不确定......

这是代码,也许你能找到我一直在监督的东西……”

//--------- Entry.m---------------

- (id) initWithCoder:(NSCoder *)aDecoder{

if ((self = [super init])) {
    self.title = [[aDecoder decodeObjectForKey:@"title"] retain];
    self.text = [[aDecoder decodeObjectForKey:@"text"] retain];
    self.created = [[aDecoder decodeObjectForKey:@"created"] retain];
}
return self;

}

- (void) encodeWithCoder:(NSCoder *)aCoder{

[aCoder encodeObject:self.title forKey:@"title"];
[aCoder encodeObject:self.text forKey:@"text"];
[aCoder encodeObject:self.created forKey:@"created"];

}

//-------------- Diary View Controller.m

- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}

- (void) writeDataToArchive {
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
                             initForWritingWithMutableData:data];

[archiver encodeObject:self.entriesArray forKey:@"entriesArray"];
[archiver finishEncoding];
BOOL result = [data writeToFile:[self dataFilePath]  atomically:YES];
[archiver release];
[data release];    
}

- (void)addItem:sender {
int count = [entriesArray count] +1;    
NSString *newEntryTitle = [NSString stringWithFormat:@"Entry %d", count];    
Entry *anEntry = [[Entry alloc] initWithTitle:newEntryTitle text:@"-" 
         created:[NSDate date]];
[entriesArray addObject:anEntry];
[self.tableView reloadData];

[anEntry release];
[self writeDataToArchive];
}

- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {     
     NSData *data = [[NSMutableData alloc]
                     initWithContentsOfFile:[self dataFilePath]];

     NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
          initForReadingWithData:data];
     NSMutableArray *array = [unarchiver decodeObjectForKey:@"entriesArray"];     
     entriesArray = [array mutableCopy];
     [array release];
     [unarchiver finishDecoding];
     [unarchiver release];
     [data release];
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
    (NSIndexPath *)indexPath
{
    // ... some other stuff
     NSUInteger row = indexPath.row;
     Entry *entry = [entriesArray objectAtIndex:row];

     cell.textLabel.text = entry.title;
     return cell;
}

非常感谢。

4

2 回答 2

3

当你用 NSKeyedUnarchivers 读回一个数组时,你总是得到一个不可变的副本。您需要将 *array 声明为 NSArray 或一起摆脱 array 。

entriesArray = [[unarchiver decodeObjectForKey:@"entriesArray"] mutableCopy]; 

@JeremyP 指出了另一个问题。由于您没有分配或保留 *array,因此您不应该释放它。

于 2011-05-09T17:02:05.217 回答
2

你不应该因为你不拥有它 而释放它arrayviewDidLoad

请查看Cocoa 内存管理规则,因为您的代码中还有其他一些内存管理问题。尤其,

self.title = [[aDecoder decodeObjectForKey:@"title"] retain];
self.text = [[aDecoder decodeObjectForKey:@"text"] retain];
self.created = [[aDecoder decodeObjectForKey:@"created"] retain];

在您的initWithCoder:方法中,所有泄漏都假设属性是保留或复制。

于 2011-05-09T10:31:21.777 回答