我目前正在为我的 plist 创建一个控制器类。在这个 plist 中,我有一个根字典,其中包含多种类型(数字、字符串和字典),在我的控制器类中,我检查了一个 plist,然后将其添加到文档中,所以我可以读取和写入它。
从这里我阅读了我当前 plist 中的内容,并将这些值传递给我在这个类中设置的 tempvar。
这就是我的 plist 控制器类中的 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"];
任何帮助将不胜感激。