NSMutableArray 具有自定义对象无效属性
我在 NSUserDefault 中保存了一个带有自定义对象的 nsmutable 数组。但是当我检索时,我得到了一个包含对象的数组,但是当我尝试迭代对象时,应用程序崩溃并且在本地调试器中我变得无效
CFStringRef 和日志 -[消息标题]:无法识别的选择器发送到实例 0x187900。
我的自定义对象遵循 NSCoding 协议,如果我打印出数组,则会以十六进制显示该对象和内存分配。
我的留言。我已将名称更改为用于测试的属性:
#import "Message.h"
@implementation Message
@synthesize tittel, adresse, dato;
static NSString *titleKey = @"title";
static NSString *urlKey = @"url";
static NSString *dateKey = @"date";
- (id)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (self != nil) {
self.tittel = [decoder decodeObjectForKey:titleKey];
self.adresse = [decoder decodeObjectForKey:urlKey];
self.dato = [decoder decodeObjectForKey:dateKey];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self.tittel forKey:titleKey];
[encoder encodeObject:self.adresse forKey:urlKey];
[encoder encodeObject:self.dato forKey:dateKey];
}
-(void)dealloc{
[dato release];
[tittel release];
[adresse release];
[super dealloc];
}
@end
我保存并尝试检索对象的方法:-(
void)saveToFile:(NSMutableArray *)incoming{
NSUserDefaults *standardUserDefault = [NSUserDefaults standardUserDefaults];
if (standardUserDefault) {
[standardUserDefault setObject:[NSKeyedArchiver archivedDataWithRootObject:[NSArray arrayWithArray:incoming]] forKey:@"Messages"];
[standardUserDefault synchronize];
}
NSLog(@"Messages array saved. (%d message in array)",[incoming count]);
}
-(NSMutableArray*)returnFromFile{
NSUserDefaults *standardUserDefault = [NSUserDefaults standardUserDefaults];
NSData * dataCheck = [[NSData alloc]initWithData:[standardUserDefault objectForKey:@"Messages"]];
NSMutableArray *retro;
if(dataCheck != nil){
NSArray *load = [NSKeyedUnarchiver unarchiveObjectWithData:dataCheck];
if(load != nil){
retro = [NSMutableArray arrayWithArray:load];
}
else
retro = [[NSMutableArray alloc]init];
}
NSLog(@"Checking saved array (%d assignments in array)",[retro count]);
for(Message *m in retro){
NSLog(@"%@", m.tittel); //It crash here.
}
return retro;
}
I have saved a nsmutable array with custom objects in the NSUserDefault. But when I am retrieving I got a array with the objects, but when I am trying to iterate through the objects the app crash and in the local debugger I got invalid
CFStringRef and the log -[Message title]: unrecognized selector sent to instance 0x187900.
My custom object follow the NSCoding protocol and If I print out the array, is showing the object and the memory allocating in hex.
My Message. I had changed name to the attributes for testing:
#import "Message.h"
@implementation Message
@synthesize tittel, adresse, dato;
static NSString *titleKey = @"title";
static NSString *urlKey = @"url";
static NSString *dateKey = @"date";
- (id)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (self != nil) {
self.tittel = [decoder decodeObjectForKey:titleKey];
self.adresse = [decoder decodeObjectForKey:urlKey];
self.dato = [decoder decodeObjectForKey:dateKey];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self.tittel forKey:titleKey];
[encoder encodeObject:self.adresse forKey:urlKey];
[encoder encodeObject:self.dato forKey:dateKey];
}
-(void)dealloc{
[dato release];
[tittel release];
[adresse release];
[super dealloc];
}
@end
Methods where I save and try to retrieve the objects:
-(void)saveToFile:(NSMutableArray *)incoming{
NSUserDefaults *standardUserDefault = [NSUserDefaults standardUserDefaults];
if (standardUserDefault) {
[standardUserDefault setObject:[NSKeyedArchiver archivedDataWithRootObject:[NSArray arrayWithArray:incoming]] forKey:@"Messages"];
[standardUserDefault synchronize];
}
NSLog(@"Messages array saved. (%d message in array)",[incoming count]);
}
-(NSMutableArray*)returnFromFile{
NSUserDefaults *standardUserDefault = [NSUserDefaults standardUserDefaults];
NSData * dataCheck = [[NSData alloc]initWithData:[standardUserDefault objectForKey:@"Messages"]];
NSMutableArray *retro;
if(dataCheck != nil){
NSArray *load = [NSKeyedUnarchiver unarchiveObjectWithData:dataCheck];
if(load != nil){
retro = [NSMutableArray arrayWithArray:load];
}
else
retro = [[NSMutableArray alloc]init];
}
NSLog(@"Checking saved array (%d assignments in array)",[retro count]);
for(Message *m in retro){
NSLog(@"%@", m.tittel); //It crash here.
}
return retro;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了!
刚上了新课,几乎一样。唯一的区别是我使用了标准 init 方法,在其中启动所有属性和 initWithCoder。它起作用了。在此之前,如果 gcc 出现错误,或者我尝试编译一个不遵循继续显示相同错误的协议的类,就会发生这种情况。有时它有助于创建完全相同或有细微差别的东西并进行编译。看起来编译器并不总是刷新内存。
I resolved it!
Just made a new class, almost the same. Only difference is that I used a standard init method where I initiate all the attributes and the initWithCoder. And it worked. It happen before that if the gcc have had an error or I try to compile a class that not following the protocols that continue to show the same error. That sometimes it help create something exactly the same or with minors difference and compile. Looks like the compiler not always flush the memory.