NSDictionary到NSArray

NSDictionary到NSArray

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

问题描述

我有一个NSDictionary,看起来像:

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

要在表视图中使用此NSDictionary项目,我必须将其传输到NSArray,对吗?

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

所以我尝试:

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

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

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

But _myArray keeps empty? What am i doing wrong?

推荐答案

除了您发布的代码的技术问题之外,您还问以下问题:

Leaving aside the technical issues with the code you posted, you asked this:

答案是:不一定. UITableViewUITableViewDataSourceUITableViewDelegate的机制没有内在的含义,这意味着您的数据必须处于数组中.您将需要实现各种方法来告诉系统表中有多少行,以及每行中显示什么数据.许多人发现用诸如数组之类的有序数据结构来回答这些问题更加自然和有效.但是您没有要求.如果您可以使用开始的字典编写代码来实现这些方法,请放心!

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. 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!

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

09-01 23:37