我需要能够从 plist 写入和保存数据。到目前为止,我已经写出了 plist,现在我需要知道如何在 plist 中创建一个特定字符串的数组。我需要访问检查的字符串并从中创建一个数组,然后我需要能够将它们分别从是更改为否。这是plist(无法发布图片抱歉):
<?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">
<dict>
<key>US</key>
<dict>
<key>Special</key>
<dict>
<key>New item</key>
<string></string>
<key>Chocolate</key>
<array>
<dict>
<key>rank</key>
<string>1</string>
<key>image</key>
<string>KingSzRend_Rs4PBC_25%.jpg</string>
<key>name</key>
<string>Reese's PBC</string>
<key>size</key>
<string>King</string>
<key>weight</key>
<string>2.80 oz</string>
<key>barcode</key>
<string>3400000480</string>
<key>checked</key>
<string>NO</string>
</dict>
<dict>
<key>rank</key>
<string>2</string>
<key>image</key>
<string>KingSzRend_KK_25%.jpg</string>
<key>name</key>
<string>Kit Kat</string>
<key>size</key>
<string>King</string>
<key>weight</key>
<string>3.00 oz</string>
<key>barcode</key>
<string>3400000229</string>
<key>checked</key>
<string>NO</string>
</dict>
这是代码:
-(void) setupPlistAccess
{
// get paths from ProductList.plist
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"ProductList.plist"];
// check to see if Data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:@"ProductList" ofType:@"plist"];
}
// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property list into dictionary object
_productList = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!_productList)
{
NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}
self.checkedOrNot = [NSMutableArray arrayWithArray:[_productList objectForKey:@"checked"]];
}
如您所见,我尝试通过获取对象 ForKey:@"checked" 来创建数组,但是当 NSLog 运行时我得到 null