开发者

Reading Writing to plist Iphone

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 t开发者_开发技巧he 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!!


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.


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

Sample


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜