NSKeyedUnarchiver 对象被破坏?
我有一个保存到 NSUserDefaults 的数组,其中包含我的自定义类分配的数组,它符合 NSCoding 协议。数组正确保存和加载,我可以验证检索到的数组的第一个对象是否属于Assignment 类。当我尝试访问数组中赋值对象的 ivars 时,就会出现问题。它崩溃了,我收到以下错误:
*** -[CFString respondsToSelector:]: message sent to deallocated instance 0x3948d60
这是我用来保存到用户默认值的代码。请注意,我还检索并检查保存的对象以进行调试。
-(void)saveToUserDefaults:(NSArray*)myArray
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject:myArray] forKey:@"Assignments"];
[standardUserDefaults synchronize];
}
NSLog(@"Assignments array saved. (%d assignments in array)",[myArray count]);
NSData *dataCheck = [[NSData alloc] initWithData:[standardUserDefaults objectForKey:@"Assignments"]];
NSArray *arrayCheck = [[NSArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:dataCheck]];
NSLog(@"Checking saved array (%d assignments in array)",[arrayCheck count]);
if ([[arrayCheck objectAtIndex:0] isKindOfClass:[Assignment class]]) {
NSLog(@"It's of the class Assignment.");
}
Assignment *testAssignment = [[Assignment alloc] initWithAssignment:[arrayCheck objectAtIndex:0]];
NSLog(@"Title: %@ Course: %@",[testAssignment title],[testAssignment course]);
}
一切都很好,直到我分配 testAssignment,这就是崩溃发生的地方。有人有什么想法吗?
编辑:这是我在Assignment类中的NSCoding方法:
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:title forKey:@"title"];
[coder encodeObject:course forKey:@"course"];
[coder encodeObject:dueDate forKey:@"dueDate"];
[coder encodeObject:notes forKey:@"notes"];
}
- (id)initWithCoder:(NSCoder *)coder {
self = [[Assignment alloc] init];
if (self != nil)
{
title = [coder decodeObjectForKey:@"title"];
course = [coder decodeObjectForKey:@"course"];
dueDate = [coder decodeObjectForKey:@"dueDate"];
notes = [coder decodeObjectForKey:@"notes"];
}
return self;
}
I have an array that I'm saving to NSUserDefaults, containing an array of my custom class Assignment, which conforms to the NSCoding protocol. The array saves and loads properly, and I can verify that the retrieved first object of the array is of the class Assignment. The problem happens when I try to access ivars of the Assignment object in the array. It crashes and I get the following error:
*** -[CFString respondsToSelector:]: message sent to deallocated instance 0x3948d60
Here is the code I'm using to save to user defaults. Note that I am also retrieving and checking the saved object for debugging purposes.
-(void)saveToUserDefaults:(NSArray*)myArray
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject:myArray] forKey:@"Assignments"];
[standardUserDefaults synchronize];
}
NSLog(@"Assignments array saved. (%d assignments in array)",[myArray count]);
NSData *dataCheck = [[NSData alloc] initWithData:[standardUserDefaults objectForKey:@"Assignments"]];
NSArray *arrayCheck = [[NSArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:dataCheck]];
NSLog(@"Checking saved array (%d assignments in array)",[arrayCheck count]);
if ([[arrayCheck objectAtIndex:0] isKindOfClass:[Assignment class]]) {
NSLog(@"It's of the class Assignment.");
}
Assignment *testAssignment = [[Assignment alloc] initWithAssignment:[arrayCheck objectAtIndex:0]];
NSLog(@"Title: %@ Course: %@",[testAssignment title],[testAssignment course]);
}
Everything is fine until I allocate testAssignment, which is where the crash happens. Does anyone have any ideas?
EDIT: Here are my NSCoding methods in the Assignment class:
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:title forKey:@"title"];
[coder encodeObject:course forKey:@"course"];
[coder encodeObject:dueDate forKey:@"dueDate"];
[coder encodeObject:notes forKey:@"notes"];
}
- (id)initWithCoder:(NSCoder *)coder {
self = [[Assignment alloc] init];
if (self != nil)
{
title = [coder decodeObjectForKey:@"title"];
course = [coder decodeObjectForKey:@"course"];
dueDate = [coder decodeObjectForKey:@"dueDate"];
notes = [coder decodeObjectForKey:@"notes"];
}
return self;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答了我自己的问题。在 initWithCoder 中,我需要保留我正在解码的所有对象:
现在一切都工作得很好。 :)
Answered my own question. In initWithCoder, I needed to retain all of the objects I was decoding:
Everything works beautifully now. :)