我目前正在为我的 plist 创建一个 Controller 类。在这个 plist 中,我有一个根字典,其中有多种类型(数字、字符串和字典),在我的 Controller 类中,我检查一个 plist,然后将其添加到文档,以便我可以读取和写入它。
从这里,我读取当前 plist 中的内容,并将这些值传递给我在此类中设置的临时变量。
这就是我的 plist Controller 类中的 read 方法。
-(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?
}
我将数据放入变量的原因是因为后者我将使用这些值来测试我想要对我的数据库执行的检查..确保我收到正确的请求号等。
更新::我的想法是将它们添加到根字典内的字典中,就像这样。我认为这还不是很接近,但它可能会让您更好地了解我正在尝试做什么。
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;
将 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"];
请记住检查对象是否为 nil,以防键没有值。键必须完全与 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];
希望这有帮助,干杯!
关于iphone - 如何将值添加到属性列表字典内的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10003856/