我对NSCoding方法有一些问题。
当我保存我的自定义类时,似乎一切正常,它实际上在我的文档目录中创建了一个.plist
文件,并且该文件包含一个NSMutableArray
的自定义对象。
以下是我的自定义对象的界面和实现
@interface CustomObj : NSObject <NSCoding>
- (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。
一个带有2个自定义对象的tableView(对于这两个对象,单元格显示一个Image,一个带有标题的Label和一个带有Type的Label),当我单击一个单元格时,这会自动将该对象保存在“收藏夹列表”中
和一个加载收藏夹数组的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
中显示。
我只能想象我的编码器做得不好。我真的非常感谢你的答案:)