Ios – convert NSArray to NSDictionary

iosnsarraynsdictionary

i want to convert an nsarray to nsdictionary i'm using to

- (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array
{
    id objectInstance;
    NSUInteger indexKey = 0;

    NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];
    for (objectInstance in array)
        [mutableDictionary setObject:objectInstance forKey:[NSNumber numberWithUnsignedInt:indexKey++]];

    return (NSDictionary *)[mutableDictionary autorelease];
}

output result is:

{
    0 =     {
        Event = "";
        ID = 1;    };
    3 =     {
        Event = "";
        ID = 77;    };
    2 =     {
        Event = "";
        ID = 23;    };
    1 =     {
        Event = "";
        ID = 45;    };
    7 =     {
        Event = "";
        ID = 10;    };
    5 =     {
        Event = "";
        ID = 26;    };
    6 =     {
Event = "";
        ID = 27;
    };
    8 =     {
Event = "";
        ID = 28;
    };
}

After convert to nsdictionary, the order of nsdictionary isn't true to the original order, i want to display the same order in nsarray, i don't know how? can you help me?

Best Answer

NSDictionary does not have an order. Sort the keys and use them to access the entries.

Related Topic