无法弄清楚为什么我的应用程序在使用 NSKeyedArchivers / NSKeyedUnarchivers 时崩溃

发布于 2024-11-05 20:46:29 字数 2793 浏览 0 评论 0原文

我正在开发我的第一个 iPhone“日记”应用程序,它使用自定义“条目”对象来保存 NSString 标题、NSString 文本和 NSDate 创建日期。当我尝试归档 Entry 对象的 NSMutableArray,并在下次加载视图时检索它们时,应用程序崩溃了。我已经浏览了一堆使用 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;
}

非常感谢。

I am developing my first iphone 'Diary' app, which uses custom 'Entry' objects that hold an NSString title, NSString text and NSDate creationDate. When I try to archive an NSMutableArray of Entry objects, and later retrieve them the next time the view loads, the app crashes. I have gone through a bunch of sample codes and examples that use NSKeyedArchivers, but still couldn't figure out why that happens. I am guessing there is a problem with the initialization of the array that holds the entries but not sure...

Here is the code, maybe you could find something that I have persistently overseen..."

//--------- 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;
}

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是男神闪亮亮 2024-11-12 20:46:29

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

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

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

When you read an array back out with NSKeyedUnarchivers you always get an unmutable copy back. You would need to declare *array as NSArray or just get rid of array all together.

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

And @JeremyP points out another issue. Since you didn't alloc or retain *array you should not release it.

我偏爱纯白色 2024-11-12 20:46:29

您不应该在 viewDidLoad 中释放 array,因为您不拥有它。

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

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

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

You should not release array in viewDidLoad because you do not own it.

Please review the Cocoa memory management Rules because there are a couple of other memory management issues in your code. In particular,

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

in your initWithCoder: method all leak on the assumption the properties are retain or copy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文