views:

551

answers:

2

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:

  • name, of the image file (ie "myImage.png")
  • xOffsetPx
  • yOffsetPx
  • width
  • height

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?

+2  A: 

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?

Eric Petroelje
+1  A: 

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.

Jason Coco