I have a plist with an array and some strings in the array. So I am wondering how to write a new string to the array in the plist. When I use the code below to get the strings from the plist I just get null in the log.
NSMutableArray *myPrimaryinfo = [[NSMutableArray alloc] initWithContentsOfFile:@"/private/var/mobile/Library/Keyboard/UserDictionaryWordKeyPairs.plist"];
NSLog(@"%@", myPrimaryinfo);
My plist 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">
<array>
<string>Test string1</string>
<string>Test string2</string>
<string>Test string3</string>
<string>Test string4</string>
</array>
开发者_高级运维</plist>
get your path of the file like below and try..
NSString *path = [[NSBundle mainBundle] pathForResource:@"DrinkArray" ofType:@"plist"];
You can't do that on iOS, because the file system is opaque outside of your app's sandbox. For more info on what's available to your app in the iOS filesystem, read this:
http://developer.apple.com/library/iOS/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/RuntimeEnvironment/RuntimeEnvironment.html#//apple_ref/doc/uid/TP40007072-CH2-SW12
Instead of trying to access portions of the filesystem that are unavailable to your app, consider including the plist file in your app's bundle. If you want the app to be able to dynamically modify the plist, you can save a copy of it to the Documents directory parallel to the app bundle, and from then on read the plist from there instead.
精彩评论