2012-10-05 59 views
0

我一直试图保存到一个属性列表文件,但它不工作,因为它只将对象保存到数组而不是实际的文件本身,这意味着它保存的内容不会明显持续。将字符串保存到属性列表(plist)文件中?

[[category objectAtIndex:questionCounter] replaceObjectAtIndex:5 withObject:myString]; 
[category writeToFile:filePath atomically:YES]; 

我使用这个早些时候的财产清单文件保存到文档目录,这样我可以编辑和保存对他们说:

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *plistFilePath = [documentsDirectory stringByAppendingPathComponent:fileNameFull]; 

if([[NSFileManager defaultManager] fileExistsAtPath:plistFilePath]) { 

    category = [NSMutableArray arrayWithContentsOfFile:plistFilePath]; 

    NSLog(@"Files exist."); 

} 
else { 

    filePath = [[NSBundle mainBundle] pathForResource:fileNamer ofType:@"plist"]; 
    category = [NSMutableArray arrayWithContentsOfFile:filePath]; 

    NSLog(@"Files have been created."); 

} 

我的财产名单是由阵列,在那些数组中,我试图保存我的对象(字符串)。我有一种感觉,这是微不足道的,但我无法发现它。

回答

2

当你试图保存你的数组时,你传递的是filePath,它指向你不能写入的主包目录。您想用plistFilePath代替,如下所示:

[category writeToFile:plistFilePath atomically:YES]; 
相关问题