阅读写作plist Iphone

时间:2011-03-24 09:29:02

标签: iphone objective-c xml xcode plist

我已经看过很多关于此的问题,但似乎无法找到符合我需求的问题。

我有一个看起来像这样的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中的内容数组?假设我想从文本域写出内容......

感谢您的帮助!!

3 个答案:

答案 0 :(得分:7)

问题是您可能正在尝试写入您读取的同一文件。我担心这是不可能的 - 您正在阅读应用程序包,这是只读。如果要将更改保存到磁盘,则需要将字典写入位于文档目录中的plist。

要获取Documents目录的路径,您需要执行以下操作(还有其他方法可以执行此操作):

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文件,如果存在,请从中读取,否则退回阅读应用程序包

答案 1 :(得分:0)

您可以对字典进行更改,然后使用writeToFile:

Sample

答案 2 :(得分:0)

这可以通过深拷贝数组轻松处理

-(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;
}