NSDictionary 到 NSArray?
我有一个 NSDictionary
,看起来像:
{
"Item1" = 2.4;
"Item2" = 5.5;
"Item3" = 15.6;
}
要在表视图中使用这个 NSDictionary
项目,我必须将其传输到 NSArray
,对吗? ?
所以我尝试:
NSDictionary *dict = [myDict objectForKey:@"items"];
for (id item in dict) {
[_myArray addObject:[dict objectForKey:item]];
}
但是 _myArray 保持为空?我做错了什么?
i have a NSDictionary
which looks like:
{
"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?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
撇开您发布的代码的技术问题不谈,您问的是:
答案是:不一定。
UITableView
、UITableViewDataSource
或UITableViewDelegate
的机制没有任何内在的东西意味着您的数据必须位于一个数组。您将需要实现各种方法来告诉系统表中有多少行以及每行中出现哪些数据。许多人发现使用数组等有序数据结构来回答这些问题更加自然和高效。但没有要求您必须这样做。如果您可以使用您开始使用的字典编写代码来实现这些方法,请随意!Leaving aside the technical issues with the code you posted, you asked this:
The answer to which is: not necessarily. There's nothing intrinsic to the machinery of
UITableView
,UITableViewDataSource
, orUITableViewDelegate
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!您可以创建字典中所有对象的数组,然后将其用作 TableView 的数据源。
You can create an array of all the objects inside the dictionary and then use it as a datasource for the TableView.
代码片段1:
代码片段2: 如果您想进一步添加
等等,
您还可以将字典的键值存储到数组中
Code Snippet1:
Code Snippet2: If you want to add further
And so on
Also you can store key values of dictionary to array
这里可能会发生一些事情。
您列出的字典是
myDict
吗?如果是这样,那么您没有一个带有@"items"
键的对象,并且dict
变量将为 nil。您需要直接迭代myDict
。另一件需要检查的事情是
_myArray
是否是 NSMutableArray 的有效实例。如果它为零,则addObject:
方法将默默地失败。最后要检查的是字典中的对象是否正确封装在 NSNumbers (或其他一些非原始类型)中。
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 thedict
variable will be nil. You need to iterate throughmyDict
directly.Another thing to check is if
_myArray
is a valid instance of an NSMutableArray. If it's nil, theaddObject:
method will silently fail.And a final thing to check is that the objects inside your dictionary are properly encased in NSNumbers (or some other non-primitive type).
要获取字典中的所有对象,您还可以使用
enumerateKeysAndObjectsUsingBlock:
,如下所示:To get all objects in a dictionary, you can also use
enumerateKeysAndObjectsUsingBlock:
like so:你只需要初始化你的 NSMutableArray
You just need to initialize your NSMutableArray
这段代码实际上用于将值添加到
字典
中,并根据Key
通过
将数据添加到Array
中。This code is actually used to add values to the
dictionary
andthrough
the data to anArray
According to theKey
.希望这有帮助!
Hope this helps!
在 Swift 4 中:
In Swift 4: