Thanks
Thanks

Reputation: 40329

How can I make an sort of multidimensional associative array in objective-c / UIKit?

Problem: I have a set of images, lets say 5. There is an structure that defines an "image object" in the meaning of my context.

Every such image object has this information:

The images are supposed to be shown on very specific positions in an view, so I need to store all this information in some sort of multidimensional associative array (or similar). The name of the image would be the key or ID.

I stumbled upon something like that a month ago in my 1000 pages objective-c book, but can't find it anymore. Any idea here?

Upvotes: 0

Views: 1799

Answers (2)

Jason Coco
Jason Coco

Reputation: 78363

The most simple way would be to just use a dictionary:

NSMutableDictionary *images;

NSDictionary *myPictureAttributes = [NSDictionary dictionaryWithObjects:
    [NSArray arrayWithObjects:[NSNumber numberWithFloat:30],
                              [NSNumber numberWithFloat:10],
                              [NSNumber numberWithFloat:15],
                              [NSNumber numberWithFloat:15], nil],
    forKeys:[NSArray arrayWithObjects:@"xOffsetPx",
                              @"yOffsetPx",
                              @"width",
                              @"height", nil]];
[images setObject:myPictureAttributes forKey:@"myImage.png"];

You would probably want your attributes to be constants of some kind so that compiler can warn you if you make a spelling error. You could also have a simple data object class with a few properties on it, then set those and add the object directly to the dictionary. There are a number of other ways to solve this problem, depending on your exact requirements, but NSDictionary is the basic collection class in Cocoa for associative arrays.

Upvotes: 2

Eric Petroelje
Eric Petroelje

Reputation: 60508

Unless I'm completely misunderstanding the question - why not create an actual object (or struct) out of those properties and store that as the value in your dictionary?

Upvotes: 3

Related Questions