allKeys in NSDictionary not giving keys in order

2,166 views
Skip to first unread message

Bharani Muthu

unread,
Oct 28, 2012, 9:08:40 PM10/28/12
to iphonesdkd...@googlegroups.com
Hi All,

I am using plist to load the contents of UITableView.

My plist is array of dictionaries. So, I have got this dictionary now. The keys present in the dictionary are my section headers in UITableView and values are my UITableViewCell contents.

NSMutableDictionary *specificDictionary = [self.arrayFromPlist objectAtIndex:indexPathFromListOfStudentScoreButton.row];

I am getting all my keys like below:

NSArray *allKeys = [specificDictionary allKeys];

But "allKeys" doesn't give the keys in order.It just gives the keys in a random order. I need to have to in a manner as it is present in the plist file itself.

How to order the values after obtaining from "allKeys" or "allValues" method of NSDictionary.

Please help. It's urgent.

Regards,
Bharani..

Jesse Tayler

unread,
Oct 29, 2012, 10:20:56 AM10/29/12
to iphonesdkd...@googlegroups.com
allKeys is not reliable in array ordering.

keep another array next to the dictionary and load it's keys in the order you'd like, use that array to iterate through the keys of the array.

hope that helps.
> --
> You received this message because you are subscribed to the Google Groups "iPhone SDK Development" group.
> To post to this group, send email to iphonesdkd...@googlegroups.com.
> To unsubscribe from this group, send email to iphonesdkdevelop...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/iphonesdkdevelopment?hl=en.

Bharani dharan

unread,
Oct 29, 2012, 11:32:58 AM10/29/12
to iphonesdkd...@googlegroups.com
Hi Jessy,

I am sorry. I think I couldn't grasp your point. The problem is I couldn't access my NSMutableDictionary by any index (like we'll do it for array).

Could you please provide a small pseudo code/snippet on how to access the dictionary objects by index (i.e. by loading it into an array)?

Thanks for your time.


Cheers,
Bharani :)

Jesse Tayler

unread,
Oct 29, 2012, 4:26:14 PM10/29/12
to iphonesdkd...@googlegroups.com

you want to have a dictionary of objects where you can iterate through in ordered fashion and retrieve whatever the object dictionary has for that whatever ordered index?

so, make a mutable array and add each key to that array in order you want to maintain.

make sense?

maybe something like this, where dateTitle is some key that you've processed in whatever order:


dateTitle = @"Some Title In Some Order";




orderedKeys = [NSMutableArray array];
objectDictionary = [NSMutableDictionary dictionary];

(some iteration loop)

if (![orderedKeys containsObject:dateTitle]) {
[orderedKeys addObject:dateTitle];
[objectDictionary setObject:[NSMutableArray array] forKey:dateTitle];
}

NSMutableArray *setArray = [objectDictionary valueForKeyPath:dateTitle];
[setArray addObject:managedObject];


then later if you iterate through orderedKeys and ask the objectDictionary for the valueForKey:whateverIterationKey you'll get the dictionary element you want, in the order you want.

ya?

Bharani Muthu

unread,
Oct 30, 2012, 3:09:55 AM10/30/12
to iphonesdkd...@googlegroups.com
Hi Jessy,

I would like to answer my own question. After getting the ordered keys from the method, i just got the objects from them by using "objectForKey:" method.

Here is the code snippet. Hope it might help someone.

   NSArray *keysUpdated = [dictFromListOfStudents allKeys];
    NSLog(@"keysUpdated not ordered in cell for row %@", keysUpdated);
    NSLog(@"dictFromListOfStudents in cell for row %@", dictFromListOfStudents); // this will be in wrong order....

    

    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
    NSArray *descriptors = [NSArray arrayWithObject:descriptor];
    NSLog(@"descriptors %@", descriptors);
    [descriptor release];

    

    NSArray *reverseOrderToSortValuesFromKeys1 = [keysUpdated sortedArrayUsingDescriptors:descriptors];
    NSLog(@"reverseOrder in cell for row %@", reverseOrderToSortValuesFromKeys1);

    

    NSString *keyGot = [reverseOrderToSortValuesFromKeys1 objectAtIndex:indexPath.section];
    NSLog(@"keyGot %@", keyGot);
    [dictFromListOfStudents objectForKey:keyGot];

    

   // NSString *valueString2 = [allValues2 objectAtIndex:indexPath.section]; // allValues2 before
    NSString *valueString2 = [dictFromListOfStudents objectForKey:keyGot];
    NSLog(@"valueString %@", valueString2);

Regards,
Bharani..


On 30 Oct, 2012, at 12:07 PM, Bharani Muthu <barane...@gmail.com> wrote:

Hi Jessy,

Thanks for your timely help. I have managed to sort the section headers as follows:

 NSArray *allKeys = [specificDictionary allKeys];
 NSLog(@"all keys titleForHeaderInSection is %@", allKeys); // not ordered

[(    "30/10/2012 11:34:05",
    "30/10/2012 11:34:16",
    "30/10/2012 11:34:28",
    "30/10/2012 11:34:41",
    "30/10/2012 11:06:06"
)
]

    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
    NSArray *descriptors = [NSArray arrayWithObject:descriptor];
    NSLog(@"descriptors %@", descriptors);
    [descriptor release];

    

    NSArray *reverseOrder = [allKeys sortedArrayUsingDescriptors:descriptors];
    NSLog(@"reverseOrder %@", reverseOrder); // ordered

reverseOrder (
    "30/10/2012 11:34:41",
    "30/10/2012 11:34:28",
    "30/10/2012 11:34:16",
    "30/10/2012 11:34:05",
    "30/10/2012 11:06:06"
)

But I am finding difficulty in implementing this in "allValues" where my array looks like the below:

allValues (
    "Sound B,Word Medial,50%,30/10/2012 11:34",
    "Sound B,Word Initial Single,100%,30/10/2012 11:06"
)

How could I sort these elements in the array (according to date present in it i.e. "30/12/2012 11:34").

Kindly help.

Regards,
Bharani..

Bharani Muthu

unread,
Oct 30, 2012, 12:07:47 AM10/30/12
to iphonesdkd...@googlegroups.com
Hi Jessy,

Thanks for your timely help. I have managed to sort the section headers as follows:

 NSArray *allKeys = [specificDictionary allKeys];
Reply all
Reply to author
Forward
0 new messages