NSDictionary 到 NSArray?

发布于 2024-08-22 03:25:26 字数 409 浏览 10 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

给我一枪 2024-08-29 03:25:26
NSArray * values = [dictionary allValues];
NSArray * values = [dictionary allValues];
空心空情空意 2024-08-29 03:25:26
NSArray *keys = [dictionary allKeys];
NSArray *values = [dictionary allValues];
NSArray *keys = [dictionary allKeys];
NSArray *values = [dictionary allValues];
你没皮卡萌 2024-08-29 03:25:26

撇开您发布的代码的技术问题不谈,您问的是:

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

答案是:不一定。 UITableViewUITableViewDataSourceUITableViewDelegate 的机制没有任何内在的东西意味着您的数据必须位于一个数组。您将需要实现各种方法来告诉系统表中有多少行以及每行中出现哪些数据。许多人发现使用数组等有序数据结构来回答这些问题更加自然和高效。但没有要求您必须这样做。如果您可以使用您开始使用的字典编写代码来实现这些方法,请随意!

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?

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!

○愚か者の日 2024-08-29 03:25:26

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

NSArray *aValuesArray = [yourDict allValues];

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

NSArray *aValuesArray = [yourDict allValues];
陌上芳菲 2024-08-29 03:25:26

代码片段1:

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

代码片段2: 如果您想进一步添加

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

等等,

您还可以将字典的键值存储到数组中

NSArray *keys = [dictionary allKeys];

Code Snippet1:

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

Code Snippet2: If you want to add further

[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];
丑丑阿 2024-08-29 03:25:26

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

您列出的字典是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 the dict variable will be nil. You need to iterate through myDict directly.

Another thing to check is if _myArray is a valid instance of an NSMutableArray. If it's nil, the addObject: 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).

み零 2024-08-29 03:25:26

要获取字典中的所有对象,您还可以使用 enumerateKeysAndObjectsUsingBlock: ,如下所示:

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

To get all objects in a dictionary, you can also use enumerateKeysAndObjectsUsingBlock: like so:

NSMutableArray *yourArray = [NSMutableArray arrayWithCapacity:6];
[yourDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    [yourArray addObject:obj];
}];
不念旧人 2024-08-29 03:25:26

你只需要初始化你的 NSMutableArray

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

You just need to initialize your NSMutableArray

NSMutableArray  *myArray = [[NSMutableArray alloc] init];
陌若浮生 2024-08-29 03:25:26

这段代码实际上用于将值添加到字典中,并根据Key通过将数据添加到Array中。

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

This code is actually used to add values to the dictionary and through the data to an Array According to the 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);
鱼窥荷 2024-08-29 03:25:26
+ (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];
}

希望这有帮助!

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

甜扑 2024-08-29 03:25:26

在 Swift 4 中:

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

let array = Array(dict.values)

In Swift 4:

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

let array = Array(dict.values)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文