简体   繁体   English

NSDictionary到NSArray?

[英]NSDictionary to NSArray?

i have a NSDictionary which looks like: 我有一个NSDictionary看起来像:

{
"Item1" = 2.4;
"Item2" = 5.5;
"Item3" = 15.6;
}

To use this NSDictionary Items in a Table View i have to transfer it to a NSArray , am i right? 要在表视图中使用此NSDictionary项,我必须将其传输到NSArray ,对吗?

So i try: 所以我尝试:

NSDictionary *dict = [myDict objectForKey:@"items"];

for (id item in dict) {
    [_myArray addObject:[dict objectForKey:item]];
}

But _myArray keeps empty? 但是_myArray保持为空? What am i doing wrong? 我究竟做错了什么?

NSArray * values = [dictionary allValues];
NSArray *keys = [dictionary allKeys];
NSArray *values = [dictionary allValues];

Leaving aside the technical issues with the code you posted, you asked this: 除了您发布的代码的技术问题之外,您还这样询问:

To use this Dictionary Items in a Table View i have to transfer it to a NSArray, am i right? 要在表视图中使用此词典项,我必须将其传输到NSArray,对吗?

The answer to which is: not necessarily. 答案是:不一定。 There's nothing intrinsic to the machinery of UITableView , UITableViewDataSource , or UITableViewDelegate that means that your data has to be in an array. 没有什么内在的机械UITableViewUITableViewDataSource ,或UITableViewDelegate这意味着你的数据必须是在数组中。 You will need to implement various methods to tell the system how many rows are in your table, and what data appears in each row. 您将需要实现各种方法来告诉系统表中有多少行,以及每行中显示什么数据。 Many people find it much more natural and efficient to answer those questions with an ordered data structure like an array. 许多人发现用诸如数组之类的有序数据结构来回答这些问题更加自然和有效。 But there's no requirement that you do so. 但是并不需要您这样做。 If you can write the code to implement those methods with the dictionary you started with, feel free! 如果您可以使用开始的字典编写代码来实现这些方法,请放心!

您可以创建字典中所有对象的数组,然后将其用作TableView的数据源。

NSArray *aValuesArray = [yourDict allValues];

Code Snippet1: 代码段1:

NSMutableArray *array = [[NSMutableArray alloc] init];
NSArray * values = [dictionary allValues];
[array addObject:values];

Code Snippet2: If you want to add further 代码段2:如果要进一步添加

[array addObject:value1];
[array addObject:value2];
[array addObject:value3];

And so on 等等

Also you can store key values of dictionary to array 您也可以将字典的键值存储到数组

NSArray *keys = [dictionary allKeys];

There are a few things that could be happening here. 这里可能会发生一些事情。

Is the dictionary you have listed the myDict ? 您列出的字典是myDict吗? If so, then you don't have an object with a key of @"items" , and the dict variable will be nil. 如果是这样,则您没有键为@"items"的对象,并且dict变量将为nil。 You need to iterate through myDict directly. 您需要直接通过myDict进行迭代。

Another thing to check is if _myArray is a valid instance of an NSMutableArray. 要检查的另一件事是_myArray是否是NSMutableArray的有效实例。 If it's nil, the addObject: method will silently fail. 如果为零,则addObject:方法将静默失败。

And a final thing to check is that the objects inside your dictionary are properly encased in NSNumbers (or some other non-primitive type). 最后要检查的是,字典中的对象已正确装入NSNumbers(或某些其他非原始类型)中。

您只需要初始化NSMutableArray

NSMutableArray  *myArray = [[NSMutableArray alloc] init];

This code is actually used to add values to the dictionary and through the data to an Array According to the Key . 此代码实际上是用来值提供给加dictionarythrough所述数据到Array根据该Key

NSMutableArray *arr = [[NSMutableArray alloc]init];
NSDictionary *dicto = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Hello",@"StackOverFlow",@"Key1",@"StackExchange",@"Key2", nil];
NSLog(@"The dictonary is = %@", dicto);
arr = [dicto valueForKey:@"Key1"];
NSLog(@"The array is = %@", arr);
+ (NSArray *)getArrayListFromDictionary:(NSDictionary *)dictMain paramName:(NSString *)paramName
{
    if([dictMain isKindOfClass:[NSDictionary class]])
    {
        if ([dictMain objectForKey:paramName])
        {
            if ([[dictMain objectForKey:paramName] isKindOfClass:[NSArray class]])
            {
                NSArray *dataArray = [dictMain objectForKey:paramName];

                return dataArray;
            }
        }
    }
    return [[NSArray alloc] init];
}

Hope this helps! 希望这可以帮助!

To get all objects in a dictionary, you can also use enumerateKeysAndObjectsUsingBlock: like so: 要获取字典中的所有对象,还可以使用enumerateKeysAndObjectsUsingBlock:像这样:

NSMutableArray *yourArray = [NSMutableArray arrayWithCapacity:6];
[yourDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    [yourArray addObject:obj];
}];

In Swift 4: 在Swift 4中:

let dict = ["Item1":2.4, "Item2": 5.4, "Item3" : 6.5]

let array = Array(dict.values)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM