Example of NSDictionary with container class to store multiple values?

Discussion in 'iOS Programming' started by Jules2010, Apr 12, 2010.

  1. Jules2010 macrumors member

    Joined:
    Apr 7, 2010
    #1
    I need to store multiple values in an NSDictionary, I've discovered that you can use a container class, see this link.

    http://www.cocoabuilder.com/archive/cocoa/282022-store-more-complex-values-into-nsdictionary.html

    Heres an example container class
    Code:
    @interface My_Pair : NSObject
    {
    id first;
    id second;
    }
    + (My_Pair *)pairWith:(id)_first and:(id)_second;
    
    - (id)initWith:(id)_first and:(id)_second;
    
    - (id)first;
    - (id)second;
    @end
    Heres the code I would normally use to add one value to an NSDictionary.
    Code:
    NSMutableDictionary *dictResult = [[[NSMutableDictionary alloc] init] retain];
    [dictResult setValue:sWavResult			forKey:@"sWav"];	
    I realise I'd need to use setObject instead of setValue.

    I've read that id can be used for any type of object, however I'm not sure how to use the container class with an NSDictionary and how to add the values to it.

    I really need some kind person to provide me with an example.

    Many thanks in advance.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Why not just store the values in the dictionary using different keys?
     
  3. Jules2010 thread starter macrumors member

    Joined:
    Apr 7, 2010
    #3
    Well I need to store value(s) and key(s) which won't be unique. Also the values / keys need to be in a specific order.

    Granted I won't change this order, but I'm concerned this ordering may be lost.

    I have got some code to iterate the dictionary.
    Code:
    NSMutableDictionary* aDictionary = (initialized with some objects)
    
    for (id theKey in aDictionary) {
    	id anObject = [[aDictionary objectForKey:theKey] retain];
    }
    On the premise that I can't have none-unique keys, I'm going to need to extract a value, so it just made sense to have the values separate to start with.

    I need to store the name of a file, a number related to that file and as I say something unique for the key.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    You're right; dictionaries do not store data in any sort of order. That's what arrays are for.
     
  5. Jules2010 thread starter macrumors member

    Joined:
    Apr 7, 2010
    #5
    I'm really new to Obj-C and using mutli-dimensional arrays (I'm assuming thats what your suggestion) seems a big step at the moment.

    Besides which I don't have any examples of those either.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Maybe it's time to step back from the real coding then and make sure you understand the basics of Objective-C.
     
  7. Jules2010 thread starter macrumors member

    Joined:
    Apr 7, 2010
    #7
    Sorry, I meant arrays and structures / classes, not multi-dimensional array.
     
  8. Jules2010 thread starter macrumors member

    Joined:
    Apr 7, 2010
    #8
    Fair enough, but if I can't find an example of what I want to do, re. nsdictionay's / arrays with structures / classes, I won't be any further ahead.
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Any decent Objective-C educational material is going to show you how to create an array of objects, how to use dictionaries, etc., maybe not just specifically for how you'll be using them. Learning the basics is a matter of learning the building blocks. It's up to you and your imagination to put them together to solve your problems. You can not always rely on finding examples to show you how things are done. Sometimes you'll find something similar that can serve as inspiration. Other times, you're on your own.
     

Share This Page