阅读写作plist Iphone

问题描述:

我已经看到了很多关于这个问题,但似乎找不到一个适合我的需求。阅读写作plist Iphone

我有一个plist中,看起来像这样

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>Content</key> 
    <array> 
     <dict> 
      <key>dateAdd</key> 
      <date>2011-03-23T14:40:47Z</date> 
      <key>content</key> 
      <string>Content first</string> 
      <key>Title</key> 
      <string>Title 1</string> 
     </dict> 
     <dict> 
      <key>dateAdd</key> 
      <date>2011-03-23T14:40:47Z</date> 
      <key>content</key> 
      <string>Content here</string> 
      <key>Title</key> 
      <string>Title 2</string> 
     </dict> 
    </array> 
</dict> 
</plist> 

林取出由这样

plist中的信息在第一类中

//points to the plist 
    NSString *path = [[NSBundle mainBundle] bundlePath]; 
    NSString *url = [path stringByAppendingPathComponent:@"Details.plist"]; 

    //filling with contents of plist 
    NSDictionary *Data = [[NSDictionary alloc] initWithContentsOfFile:url]; 
    self.contentData = Data; 
    [Data release]; 

在第二类我创建此类的实例并获取信息

Petite_NotesAppDelegate *instance = (Petite_NotesAppDelegate *)[[UIApplication sharedApplication] delegate]; 

self.dataArray = [instance.contentData objectForKey:@"Content"]; 

这使得有可能获得从plist中这样

NSDictionary *Content = [self.dataArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [Content objectForKey:@"Title"]; 

我的问题是我不能够写的plist

因为我想写的内容是在plist中的内容数组内容,然后在项目0项目1等内。我将如何写入plist内容数组内?比方说,我想写一个文本字段的内容...

感谢您的任何帮助!

+0

处理看看这有助于。 [PropertyLists/Introduction](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html#//apple_ref/doc/uid/10000048-CJBGDEGD) – 2011-03-24 09:36:18

的问题是,你可能想写信给你读取相同的文件。恐怕这是不可能的 - 您正在阅读应用程序包,这是只读。如果您想要将更改保存到磁盘,则需要将您的字典写入位于目录中的plist。

要获得的路径,你需要做以下的文档目录(也有这样做,以及其他方式):

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

// you will read and write to this path 
NSString *url = [documentsDirectory stringByAppendingPathComponent:@"Details.plist"]; 

这也意味着,当你启动应用程序,你应该检查查看Details.plist文件是否存在于您的文档目录中,如果有,请从中读取,否则回退到应用程序包的读取。

+0

谢谢很多都是这样做的,我是从另外一个地方读书写给另一个地方的。 – Tobias 2011-03-26 20:31:38

您可以更改到字典中,然后用writeToFile:

Sample

这可以通过伊斯利阵列deepcopy的

-(void) WriteOverSamePlist : (NSNumber *) num { 
    NSString *appPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"storedRowIds.plist"]; 
    NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:[self ReadArrayFromPlist] copyItems:YES]; //[[NSMutableArray alloc] initWithObjects:@"this", @"is" , nil]; 
    [arr addObject:num]; 
    BOOL fileExists = [arr writeToFile:appPath atomically:YES]; 

    if(fileExists) { 
     NSLog(@"successfully written"); 
    } else { 
     NSLog(@"failed to write"); 
    } 
} 


-(NSMutableArray*) ReadArrayFromPlist { 
    NSString *appPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"storedRowIds.plist"]; 
    NSMutableArray *coolArray = [NSMutableArray arrayWithContentsOfFile:appPath]; 
    return coolArray; 
}