NSMutableArray 具有自定义对象无效属性

发布于 2024-12-04 16:29:21 字数 2201 浏览 4 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(1

醉态萌生 2024-12-11 16:29:21

我解决了!

刚上了新课,几乎一样。唯一的区别是我使用了标准 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.

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