阅读 写入 plist Iphone

发布于 2024-10-26 06:17:42 字数 2003 浏览 2 评论 0原文

我已经看到了很多与此相关的问题,但似乎找不到适合我需要的问题。

我有一个看起来像这样的 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"];

My问题是我无法写入 plist

因为我想写入的内容位于 plist 数组 Content 中,然后位于 item 0 item 1 等内部。我如何写入 plist 中的 content 数组内部?假设我想从文本字段写入内容...

感谢您的帮助!

I have seen quite a few questions about this but cant seem to find a one that fits my needs.

I have a plist that looks like this

<?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>

Im fetching the information from the plist like this

In the first class

//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];

In the second class i create an instance of this class and fetch the information

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

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

This makes it possible to get the content from the plist like this

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

My problem is i am not able to write to the plist

Since the content i want to write is in the plist array Content and then inside the item 0 item 1 etc. How would i write to inside the content array in the plist? Lets say i want to write content from a textfield...

Thanks for any help!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏至、离别 2024-11-02 06:17:42

问题在于您可能正在尝试写入与读取的同一个文件。恐怕这是不可能的 - 您正在从只读应用程序包中读取内容。如果要将更改保存到磁盘,则需要将字典写入位于 Documents 目录中的 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 文件是否存在在您的文档目录中,如果存在,请从中读取,否则退回到从应用程序包中读取

The problem is that you are probably trying to write to the same file that you read from. I'm afraid that's not possible - you are reading from the application bundle which is read only. If you want to save your changes to disk you need to write your dictionary to a plist located in the Documents directory.

To get the path to the Documents directory you need to do the following (there are other ways of doing this as well):

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"];

This also means that when you start your application you should check to see if the Details.plist file exists in your Documents directory, if it does, read from it, otherwise fall back to reading from the the application bundle.

眼眸里的那抹悲凉 2024-11-02 06:17:42

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

示例

You can make changes to the dictionary and then use writeToFile:

Sample

素衣风尘叹 2024-11-02 06:17:42

这可以通过 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;
}

This can easly handle by deepcopy array

-(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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文