NSKeyedArchiver和NSKeyedUnarchiver与NSMutableArray [英] NSKeyedArchiver and NSKeyedUnarchiver with NSMutableArray

查看:204
本文介绍了NSKeyedArchiver和NSKeyedUnarchiver与NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这不是一个事实,我在这里使用一个Mutable数组,但这一个是困扰我,所以这不会让我惊讶,如果是这样的情况。



背景:



我创建了一个小数据库,它本质上是一个包含自定义对象的NSMutableArray,我们可以调用recordObjects。我设置了数组:

  database = [[NSMutableArray alloc] init]; 

,我的自定义对象称为recordObject,包含以下变量和inits:

  NSString * name; 
int anInt;
Bool aBool;

我也合成了方法,所以我可以像下面这样调用:

  aString = [[database objectAtIndex:someIndex] name]; 

并添加了我的控制器类的方法来添加,删除和选择要显示的单个记录。



接下来,我设置了我的recordObject类(NSObject的子类)来使用NSCoder(通过包含在@接口指令,并在实现文件中添加了以下自定义编码器和解码器方法:

   - (void)encodeWithCoder: *编码器{
[encoding encodeObject:name forKey:@recordName];
[encoding encodeInt:anInt forKey:@recordInteger];
[encoding encodeBool:aBool forKey:@ recordBool];
}

- (id)initWithCoder:(NSCoder *)decoder {
name = [decoder decodeObjectForKey:@recordName];
anInt = [decoder decodeIntForKey:@recordInteger];
aBool = [decoder decodeBoolForKey:@recordBool];
}

为了编写文件,我使用了以下内容:

  NSKeyedArchiver archiveRootObject:database toFile:myPath]; 



当我运行程序时,一切都正常工作。对数组中的每个记录调用编码器方法,并将文件写入磁盘。使用TextEdit打开文件显示数据在那里(虽然对我来说大部分无法理解。)



问题:



我添加了以下代码将文件LOAD到我的数据库数组:

  database = [NSKeyedUnarchiver unarchiveObjectWithFile:myPath]; 

当我再次运行程序时,这次加载数据库,它APPEARS才能正常工作。我的第一个测试是使用NSMutableArray计数方法:

  x = [database count]; 

结果是X填充了文件中正确的记录数。如果在保存数据库时有5条记录,则在下一次执行程序时加载数据库后将X设置为5.



现在,这里有个大问题:



如果我尝试使用任何我的访问器方法,程序崩溃。例如,如果我尝试在加载数据库后使用以下命令:

  aString = [[database objectAtIndex:someIndex] ; 

程序崩溃并在控制台中返回以下错误:

 程序接收信号:EXC_BAD_ACCESS。 
sharedlibrary apply-load-rules all

我的解释是数据未加载并且由于某种原因而被正确地初始化到数据库数组中,但是对于我的生活我不能弄清楚我在哪里出错了。



,我实现的一切都来自Stephen G. Kochan的书Programming in Objective-C



任何想法都将非常感激!


方法未完全实现。您必须调用 [super init] 并返回 self 。您还必须 copy retain 字符串对象,否则将自动释放:

   - (id)initWithCoder:(NSCoder *)decoder 
{
self = [super init];
if(self)
{
name = [[decoder decodeObjectForKey:@recordName] copy];
anInt = [decoder decodeIntForKey:@recordInteger];
aBool = [decoder decodeBoolForKey:@recordBool];
}
return self;
}

另一个问题是这行:

  database = [NSKeyedUnarchiver unarchiveObjectWithFile:myPath]; 

这很好,除了两件事:


  1. 您没有持有对象的引用,因此将自动释放

  2. NSKeyedArchiver 返回一个不可变对象,在这种情况下为 NSArray ,而不是 NSMutableArray 。 >

您需要这样做:

  database = [[NSKeyedUnarchiver unarchiveObjectWithFile:myPath] mutableCopy]; 

这将保留对象(因为它是一个副本),并使对象成为 NSMutableArray


I'm hoping this isn't something to do with the fact that I'm using a Mutable array here, but this one is baffling me so it wouldn't surprise me if that were the case.

BACKGROUND:

I have made a small database which is essentially an NSMutableArray containing custom objects, which we can call recordObjects. I set up the array:

database = [[NSMutableArray alloc] init];

and my custom object, called "recordObject" contains the following variables and inits:

NSString *name;
int       anInt;
Bool      aBool;

I also synthesized methods so I can make calls like:

aString = [[database objectAtIndex:someIndex] name];

And added methods to my controller class to add, remove, and select the individual records for display. So far everything works correctly and exactly as expected.

Next, I've set up my recordObject class (subclass of NSObject) to use the NSCoder (by including in the @interface directive, and have added the following custom encoder and decoder methods in the implementation file:

-(void) encodeWithCoder: (NSCoder *) encoder {
    [encoder encodeObject: name  forKey: @"recordName"];
    [encoder encodeInt:    anInt forKey: @"recordInteger"];
    [encoder encodeBool:   aBool forKey: @"recordBool"];
}

-(id) initWithCoder: (NSCoder *) decoder {
    name    = [decoder decodeObjectForKey: @"recordName"];
    anInt   = [decoder decodeIntForKey:    @"recordInteger"];
    aBool   = [decoder decodeBoolForKey:   @"recordBool"];
}

In order to write the file, I have used the following:

[NSKeyedArchiver archiveRootObject:database toFile:myPath];

When I run the program, everything APPEARS to work correctly. The encoder method is called for each of the records in the array and the file is written to disk. Opening the file with TextEdit shows that the data is there (though mostly unintelligible to me.)

THE PROBLEM:

Here's where I run into a snag.

I added the following code to LOAD the file into my database array:

database = [NSKeyedUnarchiver unarchiveObjectWithFile:myPath];

When I run the program again, this time Loading the database, it APPEARS to work correctly. My first test was to use the NSMutableArray count method:

x = [database count];

The result was that X is filled with the correct number of records in the file. If there were 5 records when I saved the database, X is set to 5 after loading the database on the next execution of the program.

Now, here's the big problem:

The program crashes if I try to use ANY of my accessor methods. For example, if I try to use the following after loading the database:

aString = [[database objectAtIndex:someIndex] name];

the program crashes and returns the following error in the console:

Program received signal:  "EXC_BAD_ACCESS".
sharedlibrary apply-load-rules all

My interpretation is that the data is not being loaded and initialized into the database array correctly for some reason, but for the life of me I can't figure out where I've gone wrong here.

As a side note, everything I've implemented came from Stephen G. Kochan's book "Programming in Objective-C"

Any ideas would be greatly appreciated!

解决方案

There are a few problems with your code.

Your initWithCoder: method is not fully implemented. You must call [super init] and return self. You must also copy or retain the string object, otherwise it will be autoreleased:

- (id)initWithCoder:(NSCoder *)decoder 
{
    self = [super init];
    if(self)
    {
         name    = [[decoder decodeObjectForKey: @"recordName"] copy];
         anInt   = [decoder decodeIntForKey:    @"recordInteger"];
         aBool   = [decoder decodeBoolForKey:   @"recordBool"];
    }
    return self;
}

The other problem is with this line:

database = [NSKeyedUnarchiver unarchiveObjectWithFile:myPath];

That is fine, except for two things:

  1. You're not holding a reference to the object, so it will be autoreleased.
  2. NSKeyedArchiver returns an immutable object, in this case an NSArray and not an NSMutableArray.

You need to do this:

database = [[NSKeyedUnarchiver unarchiveObjectWithFile:myPath] mutableCopy];

That will both retain the object (because it's a copy) and make the object an NSMutableArray.

这篇关于NSKeyedArchiver和NSKeyedUnarchiver与NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆