NSDictionary objectForKey 到 var 名称 [英] NSDictionary objectForKey to var name

查看:62
本文介绍了NSDictionary objectForKey 到 var 名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过从 NSDictionary .plist 中读取的键的名称来动态命名变量(我的具体是一个字符数组)?

Is it possible to dynamically name a variable (mine is specifically a char array) by the name of a key that is read from NSDicttionary .plist?

NSMutableDictionary *readDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"path/to/plist"];
    NSDictionary * getKeys = [readDict objectForKey:@"info"];
    for (id key in getKeys) {
        NSLog(@"%@ %@", key, [getKeys objectForKey:key]);
        // output key1 some data in here== (repeats with key2, etc.)
        NSData *theData = [getKeys objectForKey:key];
        const char *hexData = theData.bytes;
                      |
                      |__ so for each iteration in the loop it would be: 
                          i.e. key1, key2... to match the plist key names
    }                     instead of a static variable name.

列表:

<dict>
    <key>info</key>
    <dict>
        <key>key1</key>
        <data>
        some data in here==
        </data>
        <key>key2</key>
        <data>
        some data in here==
        </data>
    </dict>
</dict>

推荐答案

简短回答:否

说明:

您正在声明一个局部变量,其作用域就是声明它的循环.此类变量具有分配的静态编译时名称.

You are declaring a local variable whose scope is just the loop it is declared in. Such variables have static compile-time names assigned.

此外,动态更改名称没有意义,变量的名称不会影响其内容或更改其生命周期或作用域 - 即使您可以更改名称,该变量仍然只存在于一次迭代中循环,在迭代结束时变量被销毁,在下一次迭代开始时创建一个新变量.

Furthermore there is no point in changing the name dynamically, the name of a variable does not effect its contents or change its lifetime or scope - even if you could change the name the variable would still only exist for a single iteration of the loop, at the end of an iteration the variable is destroyed, at the start of the next iteration a new variable is created.

以防万一:

有时人们会问他们是否可以创建一个具有动态名称的长期变量,以便他们以后可以通过名称找到它.虽然您仍然不能这样做(因为变量具有编译时名称),但这种情况下的标准建议是使用 NSMutableDictionary - 字典是从键(通常是字符串)到 any 对象类型(间接包括任何值类型;例如,char *;因为它们可以包装为对象).如果这是你的目标——创建一个命名"字符指针的集合——那么这就是要走的路.但是,您需要确保自己这些字符指针所指的任何内容也都存在,在您的情况下,这要求您保留这些指针所指周围的 NSData 对象(您可以将这些对象保持在相同的位置)字典,例如创建一个具有 NSData *char * 属性的类,并将其实例存储在您的字典中.

Sometimes people ask if they can create a long-lived variable with a dynamic name, so they can find it by name later. While you still cannot do that (as variables have compile-time names) the standard suggestion for this case is to use an NSMutableDictionary - dictionaries are a runtime mapping from a key, typically a string, to any object type (which indirectly includes any value type; such as, say, a char *; as they can be wrapped as objects). If this is your goal - to create a collection of "named" character pointers - then this is the route to take. However you will need to ensure yourself that whatever these character pointers refer to also stays around, which in your case requires you to keep the NSData objects around these pointers refer to (you can keep these obkjects in the same dictionary, e.g. create a class with NSData * and char * properties and store instance of this in your dictionary.

这篇关于NSDictionary objectForKey 到 var 名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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