Objective c 保存到plist文件问题
由于某种原因,我无法将新数据保存到plist文件。这是我用来保存数据的代码:Objective c 保存到plist文件问题,objective-c,nsmutablearray,plist,nsmutabledictionary,Objective C,Nsmutablearray,Plist,Nsmutabledictionary,由于某种原因,我无法将新数据保存到plist文件。这是我用来保存数据的代码: -(void)saveData:(NSMutableDictionary *)dictionaryData toFile:(NSString *)filename { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir
-(void)saveData:(NSMutableDictionary *)dictionaryData toFile:(NSString *)filename {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *path = [docDir stringByAppendingPathComponent:filename];
NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:path];
[data addObject:dictionaryData];
[data writeToFile:filename atomically:YES];
}
这是我用来将文件从捆绑包复制到应用程序目录的代码,以防它不存在:
-(NSMutableArray *)loadFromFile:(NSString *)filename {
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *path = [docDir stringByAppendingPathComponent:filename];
NSFileManager *fileMgr = [NSFileManager defaultManager];
if(![fileMgr fileExistsAtPath:path]) {
NSArray *fileArray = [filename componentsSeparatedByString:@"."];
NSString *name = [fileArray objectAtIndex:0];
NSString *ext = [fileArray objectAtIndex:1];
NSString *bundle = [[NSBundle mainBundle] pathForResource:name ofType:ext];
[fileMgr copyItemAtPath:bundle toPath:path error:&error];
}
NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:path];
return data;
}
由于某些原因,我无法将新数据保存到plist文件。当我尝试将新的NSMutableDictionary对象添加到plist文件(使用saveData:toFile:method)中,然后使用plist文件数据重新加载数组变量时,没有新对象。我做错什么了吗
这是我加载plist文件的方式:
@property (nonatomic, strong) NSMutableArray *modules;
@property (nonatomic, strong) NSMutableDictionary *module;
- (void)viewDidLoad {
[super viewDidLoad];
self.modules = [self loadFromFile:@"ModulesList.plist"];
self.module = [self.modules objectAtIndex:0];
for (int i = 0; i < self.modules.count; i++ ) {
NSLog(@"Modules array from plist file, module at index %i : %@",i, [self.modules objectAtIndex:i]);
}
@property(非原子,强)NSMutableArray*模块;
@属性(非原子,强)NSMutableDictionary*模块;
-(无效)viewDidLoad{
[超级视图下载];
self.modules=[self-loadFromFile:@“ModulesList.plist”];
self.module=[self.modules objectAtIndex:0];
对于(int i=0;i
出于测试目的,我使用以下代码添加新的模块对象:
- (IBAction)leftButton:(id)sender {
NSString *mytitle = [[NSString alloc] initWithFormat:@"my title"];
NSString *myauthor = [[NSString alloc] initWithFormat:@"my author2"];
NSUInteger myean = 22023423;
NSString *mytitle2 = [[NSString alloc] initWithFormat:@"my title 2"];
NSString *myauthor2 = [[NSString alloc] initWithFormat:@"my author2"];
NSUInteger myean2 = 29032432;
NSString *mytitle3 = [[NSString alloc] initWithFormat:@"my title 3"];
NSString *myauthor3 = [[NSString alloc] initWithFormat:@"my author 3"];
NSUInteger myean3 = 21023423;
NSMutableDictionary *mybook = [[NSMutableDictionary alloc] init];
NSMutableDictionary *mybook2 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *mybook3 = [[NSMutableDictionary alloc] init];
[mybook setObject:mytitle forKey:@"title"];
[mybook setObject:myauthor forKey:@"author"];
[mybook setObject:[NSNumber numberWithInteger:myean] forKey:@"ean"];
[mybook2 setObject:mytitle2 forKey:@"title"];
[mybook2 setObject:myauthor2 forKey:@"author"];
[mybook2 setObject:[NSNumber numberWithInteger:myean2] forKey:@"ean"];
[mybook3 setObject:mytitle3 forKey:@"title"];
[mybook3 setObject:myauthor3 forKey:@"author"];
[mybook3 setObject:[NSNumber numberWithInteger:myean3] forKey:@"ean"];
NSMutableArray *mybooks = [[NSMutableArray alloc] init];
[mybooks addObject:mybook];
[mybooks addObject:mybook2];
[mybooks addObject:mybook3];
[self.module setObject:mybooks forKey:@"books"];
[self.modules addObject:self.module];
for (int i = 0; i < self.modules.count; i++ ) {
NSLog(@"Modules array after add operation, module at index: %i: %@",i, [self.modules objectAtIndex:i]);
}
[self saveData:self.module toFile:@"ModulesList.plist"];
}
-(iAction)leftButton:(id)发送者{
NSString*mytitle=[[NSString alloc]initWithFormat:@“我的标题”];
NSString*myauthor=[[NSString alloc]initWithFormat:@“my author2”];
nsu=22023423;
NSString*mytitle2=[[NSString alloc]initWithFormat:@“我的标题2]”;
NSString*myauthor2=[[NSString alloc]initWithFormat:@“my author2”];
NSU整数myean2=29032432;
NSString*mytitle3=[[NSString alloc]initWithFormat:@“我的标题3]”;
NSString*myauthor3=[[NSString alloc]initWithFormat:@“我的作者3”];
nsuan3=21023423;
NSMutableDictionary*mybook=[[NSMutableDictionary alloc]init];
NSMutableDictionary*mybook2=[[NSMutableDictionary alloc]init];
NSMutableDictionary*mybook3=[[NSMutableDictionary alloc]init];
[mybook setObject:mytitle forKey:@“title”];
[mybook setObject:myauthor forKey:@“author”];
[mybook setObject:[NSNumber numberWithInteger:myean]forKey:@“ean”];
[mybook2 setObject:mytitle2 forKey:@“标题”];
[mybook2 setObject:myauthor2 forKey:@“作者”];
[mybook2 setObject:[NSNumber numberWithInteger:myean2]forKey:@“ean”];
[mybook3 setObject:mytitle3 forKey:@“title”];
[mybook3 setObject:myauthor3 forKey:@“作者”];
[mybook3 setObject:[NSNumber numberWithInteger:myean3]forKey:@“ean”];
NSMutableArray*mybooks=[[NSMutableArray alloc]init];
[mybooks添加对象:mybook];
[mybooks addObject:mybook2];
[mybooks添加对象:mybook3];
[self.module setObject:mybooks forKey:@“books”];
[self.modules添加对象:self.module];
对于(int i=0;i
当我使用按钮操作从plist重新加载self.modules数组时,我的新数据不存在:
- (IBAction)reload:(id)sender {
self.modules = [self loadFromFile:@"ModulesList.plist"];
for (int i = 0; i < self.modules.count; i++ ) {
NSLog(@"RELOAD: Modules array from plist file, module at index %i : %@",i,[self.modules objectAtIndex:i]);
}
}
-(iAction)重新加载:(id)发送方{
self.modules=[self-loadFromFile:@“ModulesList.plist”];
对于(int i=0;i
这是我的plist文件的屏幕截图:Add
NSLog(“path='%@',path)
在initWithContentsOfFile:
调用并发布什么是日志。还要在copyItemAtPath:
call.log消息:path:/var/mobile/Applications/68825CED-FED7-41F7-BAEE-F1D3E0A9299E/Documents/ModulesList.plistAnd中添加错误检查copyItemAtPath:
成功吗?是的,没有错误