开发者

How to write to plist successfully?

I am having trouble writing my file into the plist after going through many tutorials, other people's problems and attempting it for myself. I can read the plist with no problems but I cant update it. Below are my codes on how I am writing my data into the plist. Correct me if I made any mistake.

    NSString *path = [[NSBundle mainBundle] pathForResource:@"EventAddress" ofType:@"plist"]; 
    NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    NSArray* allmyData = [myDictionary  allValues];

    // creates and array to store only the event details
    NSMutableArray *data = [[NSMutableArray alloc] initWithArray:allmyData];
    [data addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:tfAddress.text, @"Address", tvEvents.text, @"Events", nil]];
    [myDictionary setValue:data forKey:@"Whampo"];
    BOOL flag = [myDictionary writeToFile:path atomically:YES];
    if (flag){
        NSLog(@"write to plist success");
    }
    NSLog(@"%@", myDictionary);
    [myDictionary release];

The path is correct, the file exists, my values in the textView and textField are in the array, but when it comes to the writeToFile, it does not reflect on the file located at the document directory.

EDIT 01:

I found this online, very similar to Nekto's suggestion. But I am thinking on how to implement my code with his. I think its pretty simple, but I cant seem to figure out how to.

NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentDirectory = [paths objectAtIndex:0];

NSString *plistDirectory = [NSString stringWithFormat:@"%@/Enterprise",documentDirectory];

NSString *mPath = [plistDirectory stringByAppendingPathComponent:@"Downloads.plist"]; 

[mDownloadsArray writeToFile:mPath atomically:YES];

iphonesdk.blogspot taken from that site.

EDIT 02:

I used Nekto's suggestion and it worked well. But I am curious why it is returning DocumentsEventAddress.plist rather than EventAddress.plist. My assumption is because of the NSString *rootPath 开发者_如何学Go= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *plistPath = [rootPath stringByAppendingString:@"EventAddress.plist"];

Where rootPath is returning Document is that right?


I'm writing to file in such way:

NSString *errorDesc = nil;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *plistPath = [rootPath stringByAppendingString:TEMPLATES_PATH];
NSDictionary *dict = [NSDictionary dictionaryWithObject:templates forKey:@"templates"];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:dict format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];
if (plistData)
{
    [plistData writeToFile:plistPath atomically:YES];
}else
{
    NSLog(@"[Error] Application Did Enter Background {saving file error}: %@", errorDesc);
    [errorDesc release];
}

Be sure to save file in app documents directory.


I don't believe you can write to a resource. Only files in the documents directory can be written to.


You can't write to files that are located in you Bundle. The app bundle is read only.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜