2012-04-04 128 views
0

我目前正在为plist.in这个plist创建一个控制器类我有一个根字典,它有几种类型(数字,字符串和字典),在我的控制器类中,我检查一个plist,然后将它添加到文档中,以便我可以读取和写入它。如何将值添加到属性列表的字典里面的字典

从这里我读了我目前的plist中的whats,并将这些值传递给我在这堂课中设置的tempvars。

这就是我的阅读方法在我的plist控制器类中的样子。

-(void) readPlistData 
{ 
    // Data.plist code 
    // get paths from root direcory 
    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:@"EngineProperties.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:@"EngineProperties" 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 liost into dictionary object 
    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc]; 
    if (!temp) 
    { 
     NSLog(@"Error reading plist: %@, format: %d", errorDesc, format); 
    } 
    // assign values 
    self.protocolSignature = [temp objectForKey:@"Protocol"]; 
    self.requestNumber = [temp objectForKey:@"RequestNumber"]; 

    //How do I add the dictionary values here? 
} 

我把数据转化为变量的原因是因为后者我将使用这些值来测试对检查我想对我的分贝执行..确保事情像我收到正确的请求数等

UPDATE ::我的想法将它们添加到根词典内的字典将是这样的。我认为这并不是很接近,但它可能会让你更好地理解我正在尝试做的事情。

self.cacheValue = [temp objectForKey:@"Cache Value"]; 
self.manufacturers = [cacheValue objectForKey:@"Manufacturers"]; 
    self.models = [cacheValue objectForKey:@"Model"]; 
    self.subModels = [cacheValue objectForKey:@"SubModels"]; 

任何帮助将不胜感激。

enter image description here

+1

你能举出一个plist的基本例子吗?在你的例子中,我没有真正关注它。这听起来像你想使用NSMutableDictionary? – rwyland 2012-04-04 01:59:44

+0

我已经更新了我的plist的屏幕截图,如上所示,您可以看到我正在试图弄清楚如何编码缓存值字典及其子值 – 2012-04-04 02:05:30

回答

1

我相信要做到以下几点:

定义您cacheValue财产在.H作为一个可变的字典。

NSMutableDictionary *cacheValue; 

序列化plistXml作为的NSMutableDictionary:

// This is the root Dictionary 
NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization propertyListWithData:plistXML options:NSPropertyListMutableContainersAndLeaves format:NSPropertyListXMLFormat_v1_0 error:&error]; 

因为一切都是可变的,你现在可以读取,更新,插入,删除词典或它的子内容的任何部分。举例来说,抓住易变词典“缓存值”就是:

self.cacheValue = [temp objectForKey:@"Cache Value"]; 

记住要检查的对象是不为零的情况下,没有导航键的值。关键需要为,正如,因为它出现在plist中。

更新于易变的字典的值很简单:

[self.cache setValue:@"New Value" forKey:@"Sub"]; 

最后,保存在根易变的字典回plist中的变化:

/* 
The flag "atomically" specifies whether the file should be written atomically or not. 
If flag is YES, the receiver is written to an auxiliary file, and then the auxiliary file is renamed to path. 
If flag is NO, the dictionary is written directly to path. 
The YES option guarantees that path will not be corrupted even if the system crashes during writing. 
*/ 
[self.temp writeToFile:plistPath atomically:YES]; 

希望这有助于干杯!

+0

好吧,谢谢我现在将尝试执行您的解决方案。我会让你知道我怎么走:) – 2012-04-04 03:24:21

+0

谢谢你教我关于'NSPropertyListMutableContainersAndLeaves'。 – 2012-04-04 03:29:42