开发者

Write strings in array to plist

开发者 https://www.devze.com 2023-04-01 18:09 出处:网络
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 ge

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.

0

精彩评论

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

关注公众号