如何在属性列表的字典中向字典添加值 [英] how to add values to a dictionary inside a dictionary of a property list

查看:997
本文介绍了如何在属性列表的字典中向字典添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在为我的plist创建一个控制器类.在这个plist中,我有一个根字典,其中有几种类型(数字,字符串和字典),在我的控制器类中,我检查一个plist然后将其添加到文档,以便我可以对其进行读写.

I am currently creating a controller class for my plist.in this plist I have a root dictionary that has several types in it (Number, String and Dictionary), In my controller class I check for a plist then add it to the documents so I can read and write to it.

从这里,我阅读当前plist中的内容,并将这些值传递给我在此类中设置的tempvar.

From here I read whats in my current plist and pass those values over to tempvars I have set up in this class.

这就是我的plist控制器类中的read方法的样子.

This is what my read method looks like in my plist controller class.

-(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?
}

我将数据放入变量的原因是因为后者我将使用这些值来对要对数据库执行的检查进行测试..确保诸如接收正确的请求编号等之类的东西.

The reason I put the data into variables is because latter I am going to use these values to test against checks I want to perform against my db.. making sure of things like i am receiving the correct request number etc.

更新::我的想法是将它们添加到根字典中的字典中,就像这样.我认为这还差得远,但可能会为您提供我正在尝试做的事情的更好线索.

UPDATE:: my idea to add them to the dictionary inside the root dictionary would be something like this. which i think is not even close but it might give you a better clue to what I am trying to do.

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

任何帮助将不胜感激.

推荐答案

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

I believe you want to do the following:

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

Define your cacheValue property in the .h as a mutable dictionary.

NSMutableDictionary *cacheValue;

将plistXml序列化为NSMutableDictionary:

Serialize the plistXml as a NSMutableDictionary:

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

由于所有内容都是可变的,因此您现在可以读取,更新,插入,删除字典或其子内容的任何部分.例如,获取可变字典缓存值"就是:

Since everything is mutable, you can now read, update, insert, delete any part of the dictionary or its subcontents. For instance, grabbing the Mutable Dictionary "Cache Value" is just:

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

请记住在没有键值的情况下检查对象是否为nil.密钥必须与出现在plist中的完全一样.

Remember to check that the object is not nil in case there isn't a value for the key. The key needs to be exactly as it appears in the plist.

在Mutable Dictionary中更新值很容易:

Updating a value in the Mutable Dictionary is easy:

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

最后,要将更改保存在根可变字典的根目录中,回到plist:

And finally, to save the changes in the root Mutable Dictionary back to the 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];

希望这会有所帮助,加油!

Hope this helps, cheers!

这篇关于如何在属性列表的字典中向字典添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆