3

I have a NSDictionary loaded from a remote PList. It contains an array of dictionaries:

(
    {
    id = "1234";
    count = "45";
    },
    {
    id = "244";
    count = "89";
    },
    {
    id = "9909";
    count = "123";
    }
)

How do I get this into an NSArray? I don't want separate arrays for each key. I just want an NSArray of NSDictionary.

There is probably a simple answer, but this has been bugging me.

Thanks

3
  • what is this " It contains an array of dictionaries: I just want an NSArray of NSDictionary". Commented Jul 28, 2011 at 17:29
  • What you have there is an array of name-values pair. Dictionaries are really good at holding onto and manipulating name-value pairs, so maybe that's what you want to do -- import the list of NVPairs into a dictionary? If not, then I think you might want to better explain what you mean by an "NSArray of NSDictionary". Commented Jul 28, 2011 at 17:30
  • The PList looks like this: <array> <dict> <key>id</key> <real>1234</real> <key>count</key> <real>123</real> </dict> <dict> <key>id</key> <real>223</real> <key>count</key> <real>2</real> </dict> <dict> <key>id</key> <real>777</real> <key>count</key> <real>15523</real> </dict> </array> The root element of the created NSDictionary is an array without a key. I'm trying to figure out how to get this array into a NSArray. If I added this array with a key, I could just use objectForKey to get the NSArray. Commented Jul 28, 2011 at 17:40

3 Answers 3

1

You say you have a dictionary containing the array. Whatever the key for the array is, just ask do [dictionary objectForKey:theKey], and you'll have the array.

EDIT: From your comments, it sounds like you just have an NSArray which is not in an NSDictionary at all. If so, you already have what you are looking for.

Sign up to request clarification or add additional context in comments.

7 Comments

There is no key for the array. That's the problem. :s
@Ty Kroll: Then you don't have "a NSDictionary loaded from a remote PList … [that] contains an array of dictionaries". Everything that's in a dictionary has a key. If there's no key for it, it's not in a dictionary. So the question is: What do you actually have, if not a dictionary?
That is odd indeed. I use NSPropertyListSerialization propertyListFromData:mutabilityOption:format:error to load NSData from a remote PList with the format above. Perhaps it should have saved me from myself. I definitely have an array of dictionaries. Printing my resulting NSDictionary shows something similar to what I showed in my first post.
@Ty Kroll: Have you tried asking the object what its class is? It sounds like you just have an NSArray.
Whoah! __NSCFArray. Crazy. Just casting it into NSArray* did the trick. Curious why I could get away with passing it around as a NSDictionary* when it was really a NSArray*.
|
0

How about:

NSArray *dicts = [NSPropertyListSerialization propertyListWithData:data
                                                           options:0
                                                            format:NSPropertyListOpenStepFormat
                                                             error:NULL];

Note that this is a deprecated plist format (XML or binary format is now preferred).

2 Comments

Is this only available in >= XCode 4?
No, it's been available for a long time.
0

have u tried [dictionary objectAtIndex:0]objectAtIndex:0]. try iterating over objectAtIndex in the outer loop.

all the best.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.