0

我目前正在为我的 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"];

任何帮助将不胜感激。

在此处输入图像描述

4

1 回答 1

1

我相信您想要执行以下操作:

将 .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"];

最后,将根 Mutable Dictionary 中的更改保存回 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];

希望这会有所帮助,干杯!

于 2012-04-04T02:40:09.867 回答