2

我对 NSCoding 方法有一些问题。当我保存我的自定义类时,似乎一切正常,它实际上.plist在我的文档目录中创建了一个文件,并且该文件包含一个NSMutableArray完整的我的自定义对象。

这是我的自定义对象的接口和实现

。H

@interface CustomObj : NSObject <NSCoding>

.m

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject: _titleCobj forKey:@"Title"];

  [aCoder encodeObject: _typeCobj forKey:@"Type"];

   [aCoder encodeObject: _imageCobj forKey:@"Image"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {

        NSString *title = [aDecoder decodeObjectForKey:@"Title"];
        self.titleCobj = title;

        NSString *type = [aDecoder decodeObjectForKey:@"Type"];
        self.typeCobj = type;

        UIImage *image = [aDecoder decodeObjectForKey:@"Image"];
        self.imageCobj = image;

    }
    return self;
}

这是我用来查找文档目录路径的代码

- (NSString *)documentsDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return documentsDirectory;
}

- (NSString *)dataFilePath
{
    return [[self documentsDirectory] stringByAppendingPathComponent:@"Favorites.plist"];
}

这里取而代之的是两种保存和加载数据的方法

- (void)load
{
    NSString *path = [self dataFilePath];

    if ([[NSFileManager defaultManager] fileExistsAtPath:path])
    {
        NSLog (@"<__________________________LOADING__________________________>");

        NSData *data = [[NSData alloc] initWithContentsOfFile:path];
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        self.favorites = [unarchiver decodeObjectForKey:@"Favorites"];
        [unarchiver finishDecoding];


    } else {

        NSLog (@"<__________________________FIRST TIME LOAD__________________________>");

        self.favorites = [[NSMutableArray alloc] initWithCapacity:20];
 }
}


- (void)save
{
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:_favorites forKey:@"Favorites"];
    [archiver finishEncoding];
    [data writeToFile:[self dataFilePath] atomically:YES];
}

在我的应用程序中,我有 2 个 TableViewController。

  1. 一个带有 2 个自定义对象的 tableView(对于两个对象,单元格都显示一个图像、一个带有标题的标签和一个带有类型的标签),当我单击一个单元格时,它会自动将该对象保存在“收藏夹列表”中。

  2. 和一个加载收藏夹数组的 tableView。当我加载数组时,它实际上具有我之前保存的正确数量的自定义对象。

但是从日志中我可以理解发生了一些奇怪的事情:

2012-04-15 20:37:52.910 Fav[565:15803] ESISTE un file e l'instance dell'array è ---> (
    "<CustomObj: 0x806d790>",
    "<CustomObj: 0x806d850>",
    "<CustomObj: 0x806d830>",
    "<CustomObj: 0x806d9d0>",
    "<CustomObj: 0x806d9d0>",
)
2012-04-15 20:37:52.910 Fav[565:15803] L'array was loaded, and it is composed by --> 5 objects
2012-04-15 20:37:52.912 Fav[565:15803] INSTANCE CustObj PER CELLA - NUMERO #0_______________
2012-04-15 20:37:52.912 Fav[565:15803] NUOVA INSTANCE - Instance title --> (null)
2012-04-15 20:37:52.912 Fav[565:15803] NUOVA INSTANCE - Instance type --> (null)
2012-04-15 20:37:52.913 Fav[565:15803] NUOVA INSTANCE - Instance image --> (null)
2012-04-15 20:37:52.914 Fav[565:15803] INSTANCE CustObj PER CELLA - NUMERO #1_______________
2012-04-15 20:37:52.914 Fav[565:15803] NUOVA INSTANCE - Instance title --> (null)
2012-04-15 20:37:52.914 Fav[565:15803] NUOVA INSTANCE - Instance type --> (null)
2012-04-15 20:37:52.914 Fav[565:15803] NUOVA INSTANCE - Instance image --> (null)
2012-04-15 20:37:52.915 Fav[565:15803] INSTANCE CustObj PER CELLA - NUMERO #2_______________
2012-04-15 20:37:52.915 Fav[565:15803] NUOVA INSTANCE - Instance title --> (null)
2012-04-15 20:37:52.915 Fav[565:15803] NUOVA INSTANCE - Instance type --> (null)
2012-04-15 20:37:52.916 Fav[565:15803] NUOVA INSTANCE - Instance image --> (null)
2012-04-15 20:37:52.916 Fav[565:15803] INSTANCE CustObj PER CELLA - NUMERO #3_______________
2012-04-15 20:37:52.917 Fav[565:15803] NUOVA INSTANCE - Instance title --> (null)
2012-04-15 20:37:52.917 Fav[565:15803] NUOVA INSTANCE - Instance type --> (null)
2012-04-15 20:37:52.917 Fav[565:15803] NUOVA INSTANCE - Instance image --> <UIImage: 0x843acf0>
2012-04-15 20:37:52.917 Fav[565:15803] INSTANCE CustObj PER CELLA - NUMERO #4_______________
2012-04-15 20:37:52.918 Fav[565:15803] NUOVA INSTANCE - Instance title --> (null)
2012-04-15 20:37:52.918 Fav[565:15803] NUOVA INSTANCE - Instance type --> (null)
2012-04-15 20:37:52.918 Fav[565:15803] NUOVA INSTANCE - Instance image --> <UIImage: 0x843acf0>

我不明白为什么ivars我所有的customObj都是零,但对于最后两个实例,实际上有图像?我的对象是相同的,但只有一个我可以实际加载图像并将其显示在tableView.

我只能想象我的编码器做错了什么。我真的提前感谢你的答案:)

4

0 回答 0