如何将值添加到属性列表的字典中的字典

我目前正在为我的plist.in plist创build一个控制器类,我有一个根字典,它有几个types(数字,string和字典),在我的控制器类我检查一个plist然后将其添加到文档,所以我可以读写它。

从这里我读了我现在的plist里的什么东西,把这些价值转嫁给我在这堂课里设定的临时演员。

这是我的读取方法在我的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? } 

我把数据放入variables的原因是因为后者我将使用这些值来对照我想对我的数据库执行的检查。确保像我收到正确的请求编号等事情

更新::我的想法将它们添加到根字典内的字典将是这样的。 我认为这不是很接近,但它可能会给你一个更好的线索,我想要做什么。

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

任何帮助将不胜感激。

在这里输入图像说明

我相信你想要做到以下几点:

将.h中的cacheValue属性定义为可变字典。

 NSMutableDictionary *cacheValue; 

作为一个NSMutableDictionary序列化plistXml:

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

由于一切都是可变的,你现在可以读取,更新,插入,删除字典或其子内容的任何部分。 例如,抓住Mutable Dictionary“Cache Value”只是:

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

请记住在没有键值的情况下检查对象是否为零。 关键需要完全按照出现在plist中。

更新Mutable Dictionary中的值很简单:

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

希望这有助于,欢呼!