write array of data to property list?
Hi there and thank you in advance for any responses.
I created a property list "myArray.plist" in my resource folder. In my code, I fill an array with numbers. How would I populate the property list with that array? Also how would I store that array data into the app document folder so it can be accessed when my app runs. any help would be appreciated!
this is what I tried doing:
NSMutableArray *testArray = [[NSMutableArray alloc]initWithObjects:@"OMG",@"YEAH",nil];
NSString *arrayPath = [[NSBundle mainBundle] pathForResource:@"myArray" ofType:@"plist"];
[testArray writeToFile:arrayPath atomically:NO];
pl开发者_如何转开发ease tell me what I am doing wrong.
The pathForResource:ofType: method looks for a resource that was bundled with your app and returns nil if it cannot find it.
In general, it's not standard behavior to write to your App Resources path for many different reasons - namely file ownership and permissions. In effect, you should try to use ~/Library/Application Support/ for user data which pertains to your application.
Also, NSURL is the preferred way to refer to file paths as of 10.6+ because of its ease of manipulation and alias support. Using NSURL for a situations like this is a good habit to get into.
At any rate, try doing something like this:
[testArray writeToURL:[NSURL fileURLWithPath:[@"~/Desktop/myArray.plist" stringByExpandingTildeInPath]] atomically:NO];
精彩评论