如何在objective-c / UIKit中创建一种多维关联数组?

时间:2020-12-03 21:36:20

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.

问题:我有一组图像,让我们说5.有一个结构定义了我的上下文意义上的“图像对象”。

Every such image object has this information:

每个这样的图像对象都有这些信息:

  • name, of the image file (ie "myImage.png")
  • 图像文件的名称(即“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.

图像应该显示在视图中的非常特定的位置,所以我需要将所有这些信息存储在某种多维关联数组(或类似)中。图像的名称将是密钥或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?

我一个月前在我的1000页客观书中偶然发现了类似的东西,但是再也找不到了。这有什么想法?

2 个解决方案

#1


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.

您可能希望您的属性是某种常量,以便编译器可以在出现拼写错误时向您发出警告。您还可以拥有一个包含一些属性的简单数据对象类,然后设置它们并将对象直接添加到字典中。根据您的具体要求,还有许多其他方法可以解决此问题,但NSDictionary是Cocoa中用于关联数组的基本集合类。

#2


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?

除非我完全误解了这个问题 - 为什么不从这些属性中创建一个实际的对象(或结构)并将其存储为字典中的值?

#1


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.

您可能希望您的属性是某种常量,以便编译器可以在出现拼写错误时向您发出警告。您还可以拥有一个包含一些属性的简单数据对象类,然后设置它们并将对象直接添加到字典中。根据您的具体要求,还有许多其他方法可以解决此问题,但NSDictionary是Cocoa中用于关联数组的基本集合类。

#2


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?

除非我完全误解了这个问题 - 为什么不从这些属性中创建一个实际的对象(或结构)并将其存储为字典中的值?