我需要能够从plist中编写和保存数据。到目前为止,我已经写出了plist,现在我需要知道如何在plist中创建一个特定字符串的数组。我需要访问已检查的字符串并从中创建一个数组,然后我需要能够从yes到no单独更改它们。 这是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
答案 0 :(得分:0)
尝试
//Method which provides the file path of plist saved in documents directory
//You need to handle the very first creation of plist typically its done from a plist included in resources bundle
- (NSString *)savedPlistPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *documentsPath = paths[0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"ProductList.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSString *path = [[NSBundle mainBundle]pathForResource:@"ProductList" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
[dict writeToFile:filePath atomically:YES];
}
return filePath;
}
- (void)setupPlistAccess
{
NSString *plistFilePath = [self savedPlistPath];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:plistFilePath];
//Mutable copies of containers are created as it's not just read-only
NSMutableDictionary *specialDictionary = [dictionary[@"Special"] mutableCopy];
NSMutableArray *chocolates = [specialDictionary[@"Chocolate"] mutableCopy];
//Get the details of chocolate
NSMutableDictionary *chocolate = [chocolates[0] mutableCopy];
//Access the checked value
NSString *checked = chocolate[@"checked"];
//Set new Value
checked = @"YES";
//Update in chocolate
chocolate[@"checked"] = checked;
//Replace the chocolate instance in chocolates array
[chocolates replaceObjectAtIndex:0 withObject:chocolate];
//Update in special dictionary
specialDictionary[@"Chocolate"] = chocolates;
//Update in root dictionary
dictionary[@"Special"] = specialDictionary;
//Write the dictionary back to plist
[dictionary writeToFile:plistFilePath atomically:YES];
}