通过NSDictionary获取您不知道'Key'的对象? IOS

时间:2023-01-22 21:03:31

I have a NSDictionary that I get from a webservice. Each object in this dictionary contains an array. I do not know how many objects there are going to be in the NSDictionary, or what the 'Key' for each object is going to be beforehand. I only know that it will be a Dictionary of arrays.

我有一个从Web服务获得的NSDictionary。此字典中的每个对象都包含一个数组。我不知道NSDictionary中会有多少个对象,或者每个对象的'Key'预先是什么。我只知道它将是一个数组字典。

How can I enumerate through the dictionary reading out the name of the Object and its content into arrays?

如何通过字典枚举将对象的名称及其内容读入数组?

All my dealings with Dictionaries so far I could use

到目前为止,我所有与字典的交易都可以使用

[anotherDict objectForKey:@"accounts"]

because I know the 'Keys; to expect beforehand.

因为我知道'钥匙;事先预料到。

Many Thanks, -Code

非常感谢,-Code

5 个解决方案

#1


0  

you can do this :

你可以这样做 :

NSArray * keys = [dictionnary allKeys];
for (NSString *key in keys) {
   NSArray *tmp = [dictionnary objectForKey:key];
}

#2


5  

NSDictionary has the method: enumerateKeysAndObjectsUsingBlock: since iOS 4.0. It's very straightforward, it receives a block object to operate on entries in the dictionary.

NSDictionary的方法有:enumerateKeysAndObjectsUsingBlock:自iOS 4.0起。它非常简单,它接收一个块对象来操作字典中的条目。

An example:

[anotherDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
   NSArray *arr = obj;
   //Do something
}

#3


1  

NSDictionary class has an instance method -allKeys which returns an NSArray with NSStrings for all the keys.

NSDictionary类有一个实例方法-allKeys,它为所有键返回带有NSStrings的NSArray。

#4


1  

Simplest Way WOuld be to fetch allKeys via the allKeys instance method

最简单的方法是通过allKeys实例方法获取allKeys

NSArray *keys = [myDictionary allKeys];

then iterating over dictionary with for each key.

然后为每个键迭代字典。

for (NSString *k in keys)
{
    id object = [myDictionary objectForKey:k];
}

You can also get keys in order as if values were sorted using

您还可以按顺序获取键,就像使用了值进行排序一样

NSArray *sortedKeys = [myDictionary keysSortedByValueUsingSelector:@selector(compare:)];

#5


0  

i am not a pro but i know that you can get all the keys of an dictionary

我不是专业人士,但我知道你可以获得字典的所有键

 NSLog(@"%@",[Your_Dictionary allKeys]);

This will give an array of keys in that dictionary.

这将给出该字典中的一系列键。

#1


0  

you can do this :

你可以这样做 :

NSArray * keys = [dictionnary allKeys];
for (NSString *key in keys) {
   NSArray *tmp = [dictionnary objectForKey:key];
}

#2


5  

NSDictionary has the method: enumerateKeysAndObjectsUsingBlock: since iOS 4.0. It's very straightforward, it receives a block object to operate on entries in the dictionary.

NSDictionary的方法有:enumerateKeysAndObjectsUsingBlock:自iOS 4.0起。它非常简单,它接收一个块对象来操作字典中的条目。

An example:

[anotherDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
   NSArray *arr = obj;
   //Do something
}

#3


1  

NSDictionary class has an instance method -allKeys which returns an NSArray with NSStrings for all the keys.

NSDictionary类有一个实例方法-allKeys,它为所有键返回带有NSStrings的NSArray。

#4


1  

Simplest Way WOuld be to fetch allKeys via the allKeys instance method

最简单的方法是通过allKeys实例方法获取allKeys

NSArray *keys = [myDictionary allKeys];

then iterating over dictionary with for each key.

然后为每个键迭代字典。

for (NSString *k in keys)
{
    id object = [myDictionary objectForKey:k];
}

You can also get keys in order as if values were sorted using

您还可以按顺序获取键,就像使用了值进行排序一样

NSArray *sortedKeys = [myDictionary keysSortedByValueUsingSelector:@selector(compare:)];

#5


0  

i am not a pro but i know that you can get all the keys of an dictionary

我不是专业人士,但我知道你可以获得字典的所有键

 NSLog(@"%@",[Your_Dictionary allKeys]);

This will give an array of keys in that dictionary.

这将给出该字典中的一系列键。