今天写项目的时候,自己在工程下新建了一个plist文件,后来有需求要给plist里面写数据,可是写入失败了.... 数组里面写进了内容,将数组写入文件的时候执行
[array writeToFile:filePatch atomically:YES];
失败....
切记:工程里,只可以读取不可以写入
解决方案:(可能有更好的解决方案,还没找到...)
// copy工程下的plist文件到Documents目录下,再对copy成功的文件进行读写就ok了
NSString *targetPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"joke.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL Exists = [fileManager fileExistsAtPath:targetPath];
if(!Exists){
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"joke" ofType:@".plist"];
NSError *error;
BOOL success = [fileManager copyItemAtPath:plistPath toPath: targetPath error:&error];
if(!success){
NSLog(@"copy失败");
}
}
NSMutableArray *jokeArray = [NSMutableArray arrayWithContentsOfFile:path];
NSDictionary *dict = @{@"content":self.contentField.text};
[jokeArray addObject:dict];
if([jokeArray writeToFile:targetPath atomically:YES]) {
NSLog(@"%@", dict);
}
[jokeArray writeToFile:filePatch atomically:YES];
网友评论