NSDictionaries in an NSMutableArray - return OBJECT

Discussion in 'iOS Programming' started by mraheel, Jan 4, 2010.

  1. mraheel macrumors regular

    Apr 18, 2009
    Hi guys,

    Im in a bit of a problem here..

    I got a mutablearray with NSDictionaries inside it in the following format.

    Im using Array as Parent because, the positions cant be changed. An Item1 will remain on the top. Hence its easier to load in a tableView. Simply assigning indexPath.row will bring in the required Dictionary.

    Thats fine,
    A situation has risen where i need to
    -- GET THE <Title> that has an <id>5</id>. and i dont know the Array index.
    All that is known to me is the <id> thats 5. and I need to get the <title> of that dictionary.

    is it possible...? any other models anyone recommending?

    using a simple array didnt workout cuz both the Sequence of Titles and their values were differing
  2. amorya macrumors 6502

    Jun 17, 2007
    The easiest way would be just to iterate through your array and compare each dictionary. However, if there's a lot of them, that could be computationally expensive.

    How about this? Make a new dictionary. Put the same dictionaries in it as in your array, and set the key of each one to be its ID. Then you can look up the dictionary you want using valueForKey:.

    If you need to know the dictionary's position in the original array, you could then call indexOfObject: on the array, and pass in the dictionary (which you've just looked up).
  3. mraheel thread starter macrumors regular

    Apr 18, 2009
    Hey, thanks for your reply..

    Im kinda confused as to what alternative you've provided. Im understanding it vaguely.
    Are you saying to "add" a new dictionary apart from the array and their dictionaries?
  4. amorya macrumors 6502

    Jun 17, 2007
    You have one array, containing dictionaries (in an order you want to preserve). I'll use your example, where you have two dictionaries, one with title FirstTitle and ID 1, one with SecondTitle and 5.

    As well as your array, create a new NSMutableDictionary. Let's call it lookupDictionary.

    NSMutableDictionary *lookupDictionary = [NSMutableDictionary dictionaryWithCapacity:5];
    Into lookupDictionary, put the two dictionaries (FirstTitle and SecondTitle) that you're going to want to find.

    [lookupDictionary setObject:firstTitle forKey:[firstTitle objectForKey:@"id"]];
    [lookupDictionary setObject:secondTitle forKey:[secondTitle objectForKey:@"id"]];
    So you've put the same objects into lookupDictionary as you already have in your array.

    Now, to search by ID, you can do

    NSDictionary *result = [lookupDictionary objectForKey:@"5"];
    NSString *title = [result objectForKey:@"title"];
    If you then wanted to know which position in your ordering array the item you just found was, you could do this:

    int position = [orderingArray indexOfObject:result];
    Does that help?

  5. mraheel thread starter macrumors regular

    Apr 18, 2009

Share This Page