Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

iphone - 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 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *plistPath = [rootPath stringByAppendingString:@"EventAddress.plist"];

Where rootPath is returning Document is that right?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...