phx
phx

Reputation: 1509

NSDictionary to NSArray?

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?

Upvotes: 34

Views: 113170

Answers (11)

Dharmesh Mansata
Dharmesh Mansata

Reputation: 4708

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

Upvotes: 1

garg
garg

Reputation: 2727

In Swift 4:

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

let array = Array(dict.values)

Upvotes: 0

onCompletion
onCompletion

Reputation: 6650

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

Upvotes: 1

Yogeesh H T
Yogeesh H T

Reputation: 2885

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

Upvotes: 3

AP Singh
AP Singh

Reputation: 11

You just need to initialize your NSMutableArray

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

Upvotes: 1

RandomGuy
RandomGuy

Reputation: 127

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

Upvotes: 4

Nikhil Dinesh
Nikhil Dinesh

Reputation: 3409

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

Upvotes: 44

pixelfreak
pixelfreak

Reputation: 17834

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

Upvotes: 1

Sixten Otto
Sixten Otto

Reputation: 14816

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!

Upvotes: 29

Dave DeLong
Dave DeLong

Reputation: 243146

NSArray * values = [dictionary allValues];

Upvotes: 224

mbauman
mbauman

Reputation: 31342

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

Upvotes: 2

Related Questions