2013-04-30 48 views
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创建数组:@ “签”,但运行的NSLog当我得到空

回答

0

enter image description here

尝试

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

}