objective-c NSDictionary 到 NSArray?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2274022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:56:36  来源:igfitidea点击:

NSDictionary to NSArray?

objective-cnsarraynsdictionary

提问by phx

i have a NSDictionarywhich looks like:

我有一个NSDictionary看起来像:

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

To use this NSDictionaryItems in a Table View i have to transfer it to a NSArray, am i right?

NSDictionary在表视图中使用此项目,我必须将其转移到 a NSArray,对吗?

So i try:

所以我尝试:

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

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

But _myArray keeps empty? What am i doing wrong?

但是 _myArray 保持为空?我究竟做错了什么?

采纳答案by Sixten Otto

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 UITableViewDelegatethat means that your data has tobe in an array. 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 requirementthat you do so. If you can write the code to implement those methods with the dictionary you started with, feel free!

答案是:不一定。没有什么固有的机械的UITableViewUITableViewDataSource或者UITableViewDelegate这意味着你的数据必须是一个数组。您将需要实现各种方法来告诉系统您的表中有多少行,以及每行中出现了哪些数据。许多人发现用像数组这样的有序数据结构来回答这些问题更加自然和高效。但是没有要求你这样做。如果您可以编写代码来使用您开始使用的字典来实现这些方法,请随意!

回答by Dave DeLong

NSArray * values = [dictionary allValues];

回答by Nikhil Dinesh

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

回答by RandomGuy

You can create an array of all the objects inside the dictionary and then use it as a datasource for the TableView.

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

NSArray *aValuesArray = [yourDict allValues];

回答by Yogeesh H T

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];

回答by Matt B.

There are a few things that could be happening here.

这里可能会发生一些事情。

Is the dictionary you have listed the myDict? If so, then you don't have an object with a key of @"items", and the dictvariable will be nil. You need to iterate through myDictdirectly.

你列出的字典是myDict?如果是这样,那么你没有某种重要对象@"items",和dict变量将是零。您需要myDict直接迭代。

Another thing to check is if _myArrayis a valid instance of an NSMutableArray. If it's nil, the addObject:method will silently fail.

要检查的另一件事是是否_myArray是 NSMutableArray 的有效实例。如果它为零,该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(或其他一些非原始类型)中。

回答by pixelfreak

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];
}];

回答by AP Singh

You just need to initialize your NSMutableArray

你只需要初始化你的 NSMutableArray

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

回答by onCompletion

This code is actually used to add values to the dictionaryand throughthe data to an ArrayAccording 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);

回答by Dharmesh Mansata

+ (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!

希望这可以帮助!