[Newbie] Good NSDictionary example?

  • Hi all,

    After an afternoon of "attempt to insert nil value" and SIGTRAPs, is
    there a simple example of creating an NSDictionary somewhere?

    I want something like :-

    string ->    string
      string
      date

    looking like

    myreference -> name
                         subject
                         date

    (about 35,000 of these records)

    Want to be able to say, for myreference="something" show me name,
    subject,date  ... the results of which end up in a table.

    Is this best represented as an array of dictionaries somehow?

    Thanks in advance,
    Keith
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Here's some very basic stuff in case it's what you're looking for.
    I'm pretty new to this as well so forgive me if I underestimated your
    needs.

        NSMutableDictionary *myDictionary = [[ NSMutableDictionary alloc]
    init];
        [ myDictionary setObject:@"Keith" forKey:@"name"];
        [ myDictionary setObject:@"hacker" forKey:@"subject"];
        [ myDictionary setObject:@"4/9/2002" forKey:@"date"];

        NSEnumerator *enumerator = [ myDictionary keyEnumerator];
        NSString *key;
        while (key = [ enumerator nextObject]) {
            printf("%s\n", [[ myDictionary objectForKey:key] UTF8String]);
        }

    And yes, an NSArray of these sounds like it would be fine.

    brian redman

    On Saturday, September 7, 2002, at 06:11 PM, Keith Pritchard wrote:

    > Hi all,
    >
    > After an afternoon of "attempt to insert nil value" and SIGTRAPs, is
    > there a simple example of creating an NSDictionary somewhere?
    >
    > I want something like :-
    >
    > string ->    string
    > string
    > date
    >
    > looking like
    >
    > myreference -> name
    > subject
    > date
    >
    > (about 35,000 of these records)
    >
    > Want to be able to say, for myreference="something" show me name,
    > subject,date  ... the results of which end up in a table.
    >
    > Is this best represented as an array of dictionaries somehow?
    >
    > Thanks in advance,
    > Keith
    > _______________________________________________
    > cocoa-dev mailing list | <cocoa-dev...>
    > Help/Unsubscribe/Archives:
    > http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    > Do not post admin requests to the list. They will be ignored.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Am Sonntag, 08.09.02 um 00:11 Uhr schrieb Keith Pritchard:

    > Want to be able to say, for myreference="something" show me name,
    > subject,date  ... the results of which end up in a table.

    Looks like a dictionary of objects to me.

    Build a class with name, subject and date properties and, create an
    object for each dataset and put it in a dictionary with <something> as
    key.

    To create an empty mutable dictionary use

    myDictionary = [NSMutableDictionary dictionaryWithCapacity:35000];

    (While you may adjust the initial capacity for greater speed, the
    dictionary will grow automatically if needed.)

    Then create your records and add them like this:

    [myDictionary setObject:anObject forKey:aKey];

    Caveats:

    - NSMutableDictionary dictionaryWithCapacity: returns an autoreleased
    dictionary. So you have to retain it, to keep it around
    - if you don't not need your datasets to perform any actions, you may
    use a dictionaries or arrays instead of your own class to hold the data
    - I'm not sure how the performance of NSDictionary is with many entries

    bye.  Andreas.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Sunday, September 8, 2002, at 01:05 , Andreas Mayer wrote:

    >> Want to be able to say, for myreference="something" show me name,
    >> subject,date  ... the results of which end up in a table.
    >
    > Looks like a dictionary of objects to me.
    >
    > Build a class with name, subject and date properties and, create an
    > object for each dataset and put it in a dictionary with <something> as
    > key.

    Or -- which seems to me as an easier way -- don't build anything, and just
    make a dictionary of dictionaries, like

    id manuallyCreated=[NSDictionary dictionaryWithObjectsAndKeys:
        [NSDictionary dictionaryWithObjectsAndKeys:
           @"Ondra",@"christian",@"Cada",@"family",[NSDate
    date],@"date",nil],@"something",
        [NSDictionary dictionaryWithObjectsAndKeys:
           @"Andreas",@"christian",@"Mayer",@"family",@"other ID",
        nil];
    ---
    Ondra Cada
    OCSoftware:    <ocs...>              http://www.ocs.cz
    private        <ondra...>            http://www.ocs.cz/oc
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Am Sonntag, 08.09.02 um 02:31 Uhr schrieb Ondra Cada:

    > Or -- which seems to me as an easier way -- don't build anything, and
    > just make a dictionary of dictionaries, like

    Yes. That's what I meant to say with this line:

    > - if you don't not need your datasets to perform any actions, you may
    > use a dictionaries or arrays instead of your own class to hold the > data

    Maybe my english is not that good, sorry.  :)

    bye.  Andreas.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Thanks everyone for that great help! Really appreciated....

    I've tried both ways... dictionary of arrays and as below, a dictionary
    of dictionaries.

    id myDict=[NSMutableDictionary dictionaryWithObjectsAndKeys:

    [NSMutableDictionary
    dictionaryWithObjectsAndKeys:@"keith",@"name",@"subject1",@"subject",@"1
    2/12/02",@"date",nil],@"123",

    [NSMutableDictionary
    dictionaryWithObjectsAndKeys:@"helen",@"name",@"subject2",@"subject",@"1
    3/12/03",@"date",nil],@"124",
            nil];

    I've added more data by doing:-
    [myDict setObject:
    [NSMutableDictionary
    dictionaryWithObjectsAndKeys:@"asd",@"name",@"subject3",@"subject",@"14/
    12/04",@"date",nil] forKey:@"125"];

    Andreas mentioned retaining it to keep it around? I tried [myDict
    retain] but ok, I suppose that was a bit optimistic :-)

    When I try to access it in different methods the values come back as
    null, so I guess it's only living in the function it's created in...

    How can I make it accessible to the whole controller?

    Thanks again for all the help,
    Keith
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Am Sonntag, 08.09.02 um 06:11 Uhr schrieb Keith Pritchard:

    > id myDict=[NSMutableDictionary dictionaryWithObjectsAndKeys:

    Unless there's a good reason not to, use a qualified type:

    NSMutableDictionary *myDict = [...

    That will help the compiler to detect errors in your code.

    > I tried [myDict retain] but ok, I suppose that was a bit optimistic :-)

    That's fine.

    But it seems, your myDict variable got out of scope.
    So the object is still there, but you have no way to access ist.

    > How can I make it accessible to the whole controller?

    You will have to declare it as a property of the controller class.

    Next you'll probably want to write setter and getter methods:

    - (void)setMyDict:(NSMutableDictionary *)newMyDict
    {
    NSMutableDictionary *oldValue = myDict;
    myDict = [newMyDict retain];
    [oldValue release];
    }

    - (NSMutableDictionary *)myDict
    {
    return myDict;
    }

    Put these methods in your public interface to access myDict from
    outside your controller.

    And don't forget to release it when the controller is destroyed:

    - (void)dealloc
    {
    [myDict release];
    ... more ...
    [super dealloc];
    }

    bye.  Andreas.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
previous month september 2002 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            
Go to today