views:

1276

answers:

3

Hey all,

i have a NSDictionary which looks like:

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

To use this Dictionary 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?

Thanks for your help!

+1  A: 

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

Matt B.
No, myDict is not the example shown above, it should show you that i have a NSDictionary of NSDictionarys.How can i check if my objects are NSNumbers?
phx
Sorry, my fault! I misuse "addObject".I want to addObjects of NSDictionarys to my Array! So i have to do it in another way!
phx
+3  A: 
NSArray * values = [dictionary allValues];
Dave DeLong
Hey,i want to add Objects to the Array, not only the values.
phx
If you read the documentation, `allValues` returns every object that would be returned by invoking `objectForKey:` for every key in the dictionary.
Dave DeLong
Wow Dave - watch the attitude! People are here to get help, not get lectured by you.
mark
+2  A: 

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!

Sixten Otto