Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a very basic question about how to write to a plist. I added a "Here.plist" file to my resources folder. The following is my code:

NSString *path = [[NSBundle mainBundle]pathForResource:@"Here" ofType:@"plist"];

NSArray *array = [[NSArray alloc]initWithObjects:@"First", @"Second", @"Third", nil];

[array writeToFile:path atomically:YES];

"Here.plist" is still empty after executing the code. I even tried using a NSDictionary as follows:

NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:@"firstValue", @"First", @"secondValue", @"Second", @"thirdValue", @"Third", nil];

[dict writeToFile:path atomically:YES];

I went through many tutorials and forums but its just not working for me. What am i doing wrong? Losing my head over this.

See Question&Answers more detail:os

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

1 Answer

You can't write into the bundle. Try writing into the documents directory. For example:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *array = [[NSArray alloc]initWithObjects:@"First", @"Second", @"Third", nil];
[array writeToFile:[documentsDirectory stringByAppendingPathComponent:@"Here.plist" atomically:YES];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...