我正在尝试将一个NSMutableArray
与对象保存到一个文件中。没有错误,当我调试代码时,对象Recipe总是有一个内容永远不会是空的,但productsList永远不会添加这个对象...我对此感到疯狂,一些想法?
#import "Utils.h"
#import "Recipe.h"
@implementation Utils
{
NSFileManager *filemgr;
NSString *docsDir;
NSArray *dirPaths;
NSString *dataFilePath;
}
-(Utils*) init {
self = [super init];
if (self) {
filemgr = [NSFileManager defaultManager];
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the data file
dataFilePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"wish.info"]];
}
return self;
}
-(NSMutableArray * )loadProducts{
return [NSKeyedUnarchiver unarchiveObjectWithFile:dataFilePath];
}
-(void) saveProducts: (NSMutableArray * ) productsList{
[NSKeyedArchiver archiveRootObject:productsList toFile:dataFilePath];
NSLog(@"Products List%@",productsList);
}
-(void) addProduct: (Recipe*) recipe{
NSMutableArray *productsList = [self loadProducts];
[productsList addObject:recipe];
[self saveProducts:productsList];
}
-(void) deleteProduct: (Recipe*) recipe{
NSMutableArray *productsList = [self loadProducts];
[productsList removeObject:recipe];
[self saveProducts:productsList];
}
-(Boolean) isSelected: (Recipe*) recipe{
return [[self loadProducts] containsObject:recipe];
}
@end