如何在plist中写入数据?

我在资源文件夹中创build了save.plist。 我直接写了一些数据(不使用编码)。 我能够读取这些数据,但是我无法通过编码写入同一个save.plist。 通过使用下面的代码,我试图写入数据,但它存储在我的.app plist中。 代码在这里

NSString *errorDesc = nil; NSPropertyListFormat format; NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"save" ofType:@"plist"]; NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath]; NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc]; if (!temp) { NSLog(errorDesc); [errorDesc release]; } // [temp setValue:@"123" forKey:@"line1"]; // [temp writeToFile:plistPath atomically: YES]; //Reading data from save.plist NSLog([temp objectForKey:@"name"]); NSLog([temp objectForKey:@"wish"]); NSNumber *num=[temp valueForKey:@"roll"]; int i=[num intValue]; printf("%d",i); //writitng the data in save.plist [temp setValue:@"green" forKey:@"color"]; [temp writeToFile:plistPath atomically: NO]; NSMutableDictionary *temp1 = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc]; NSLog([temp objectForKey:@"color"]); 

我想要的是,我想要写入的数据应该只写入保存在引用中的save.plist中。 我对这个概念是新的。 所以如果有人知道,请帮助我。 提前致谢。 🙂

我不知道我是否理解你的问题,但是如果你想在你的.app包中写入一个.plist,你可能是做错了。 如果你想存储首选项,你应该考虑使用NSUserDefaults 。
如果你真的想修改一个捆绑的.plist – 这是一些代码:

 NSString* plistPath = nil; NSFileManager* manager = [NSFileManager defaultManager]; if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Contents/Info.plist"]) { if ([manager isWritableFileAtPath:plistPath]) { NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath]; [infoDict setObject:[NSNumber numberWithBool:hidden] forKey:@"LSUIElement"]; [infoDict writeToFile:plistPath atomically:NO]; [manager changeFileAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] atPath: [[NSBundle mainBundle] bundlePath]]; } } 

更新:
Nate Flink指出,上面使用的一些NSFileManager方法已被弃用。 他用下面的replace方法发布了一个答案: https : //stackoverflow.com/a/12428472/100848

weichsel最初的真棒示例的更新版本(谢谢!)。 Xcode引发了一些警告,其中之一是NSFileManager上的弃用方法。 这里更新了iOS 5.1的非弃用方法

  NSString* plistPath = nil; NSFileManager* manager = [NSFileManager defaultManager]; if ((plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"mySpecial/PathTo.plist"])) { if ([manager isWritableFileAtPath:plistPath]) { NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath]; [infoDict setObject:@"foo object" forKey:@"fookey"]; [infoDict writeToFile:plistPath atomically:NO]; [manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:nil]; } } 

当您构build应用程序时,它将创build一个可执行文件“appName.app”,并且所有文件都在该包中生成。 因此,当应用程序正在运行时,您无法访问资源文件夹,因为所有数据都在包中(不在文件夹中)。

但是,您可以访问包含应用程序某些信息的临时文件夹。 你可以在这里find临时文件夹: 打开finder – 点击你的用户名(在地方下) – 图书馆 – 应用程序支持 – iPhone模拟器 – 用户 – 应用程序 – (在这里你可以find你的所有临时文件夹iPhone应用程序)

您可以通过以下方式访问此临时文件夹:

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

如果你命名你的文件save.plist,你可以像这样访问它:

 NSString *filePath = [documentsDirectory stringByAppendingString:@"_save.plist"]; 

然后,将文件保存到此文件path中,它将出现在名为“Documents_save.plist”的临时文件夹中。

*请注意,每次运行应用程序时,临时文件夹的名称都会有所不同。

为你推荐一本书:“开始iPhone开发 – 探索iPhone SDK”。 在第十一章中,你可以find你想要的。

总结一些其他答案:

你的问题是,你正在试图将文件写回到包含你的应用程序的文件夹中。 该文件夹在运行时不可写。 你所做的一切都很好,你只需要select一个不同的位置来写你的文件。

您可以使用NSSearchPathForDirectoriesInDomains函数为此数据find一个更合适的文件夹。

尝试这个:

 -(void)add:(NSRunningApplication *) app { if ([self contains:app]) return; [self.apps addObject:app.localizedName]; [self.apps writeToFile:self.dataFile atomically:YES]; } 

从“cocoa编程”。

你必须复制你的plist到文档目录…因为你不能保存任何东西,而不保存到文档文件….当你复制它将允许写/修改plist