1

我需要能够从 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&apos;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

4

1 回答 1

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];

}
于 2013-04-30T22:50:48.620 回答