简体   繁体   English

Objective-c:向NSMutableArray添加自定义对象

[英]Objective-c: Adding a custom object to a NSMutableArray

I usually program in java or c++ and I recently started with objective-c. 我通常使用Java或C ++进行编程,最近我开始使用Objective-C。 Looking for vectors in objective-c, I found NSMutableArray which seems to be the best option. 在Objective-C中寻找向量,我发现NSMutableArray似乎是最好的选择。 I'm working on an opengl game and I'm trying to create an NSMutableArray of textured quads for my sprites. 我正在开发一个opengl游戏,并且试图为我的sprite创建一个带纹理的四边形的NSMutableArray。 Here is the relevant code: 以下是相关代码:

I define textured quads: 我定义纹理四边形:

typedef struct {
    CGPoint geometryVertex;
    CGPoint textureVertex;
} TexturedVertex;

typedef struct {
    TexturedVertex bl;
    TexturedVertex br;    
    TexturedVertex tl;
    TexturedVertex tr;    
} TexturedQuad;

I create an array in the interface: 我在接口中创建一个数组:

@interface Sprite() {
    NSMutableArray *quads;
}

I initiate the array and I create the texturedQuads based on "width" and "height", which are the dimensions of a single sprite, and "self.textureInfo.width" and "self.textureInfo.height", which are the dimensions of the entire sprite sheet: 我初始化数组,并基于“宽度”和“高度”(它们是单个精灵的尺寸)以及“ self.textureInfo.width”和“ self.textureInfo.height”(它们的尺寸)来创建TexturedQuads。整个精灵表:

    quads = [NSMutableArray arrayWithCapacity:1];
    for(int x = 0; x < self.textureInfo.width/width; x++) {
    for(int y = 0; y < self.textureInfo.height/height; y++) {
        TexturedQuad q;
        q.bl.geometryVertex = CGPointMake(0, 0);
        q.br.geometryVertex = CGPointMake(width, 0);
        q.tl.geometryVertex = CGPointMake(0, height);
        q.tr.geometryVertex = CGPointMake(width, height);

        int x0 = (x*width)/self.textureInfo.width;
        int x1 = (x*width + width)/self.textureInfo.width;
        int y0 = (y*height)/self.textureInfo.height;
        int y1 = (y*height + height)/self.textureInfo.height;

        q.bl.textureVertex = CGPointMake(x0, y0);
        q.br.textureVertex = CGPointMake(x1, y0);
        q.tl.textureVertex = CGPointMake(x0, y1);
        q.tr.textureVertex = CGPointMake(x1, y1);

        //add q to quads
    }
    }

The problem is I don't know how to add the quad "q" to the array "quads". 问题是我不知道如何将quad“ q”添加到数组“ quads”。 Simple writing [quads addObject:q] doesn't work because the parameter should be an id not a TexturedQuad. 简单编写[quads addObject:q]不起作用,因为该参数应该是一个id而不是TexturedQuad。 I've seen examples of how to make an id from an int etc, but I don't know how to do it with an object like my TexturedQuad. 我已经看到了如何从int等中获取ID的示例,但我不知道如何使用我的TexturedQuad这样的对象来实现。

The essence of it is that you wrap your C struct in an Obj-C class. 它的本质是将C结构包装在Obj-C类中。 The Obj-C class to use is NSValue . 要使用的Obj-C类是NSValue

// assume ImaginaryNumber defined:
typedef struct {
    float real;
    float imaginary;
} ImaginaryNumber;

ImaginaryNumber miNumber;
miNumber.real = 1.1;
miNumber.imaginary = 1.41;

// encode using the type name
NSValue *miValue = [NSValue value: &miNumber withObjCType:@encode(ImaginaryNumber)]; 

ImaginaryNumber miNumber2;
[miValue getValue:&miNumber2];

See here for more information. 有关更多信息,请参见此处

As @Bersaelor pointed out, if you need better performance use pure C or switch to Obj-C++ and use vectors instead of Obj-C objects. 正如@Bersaelor指出的那样,如果需要更好的性能,请使用纯C或切换到Obj-C ++并使用向量而不是Obj-C对象。

An NSMutableArray takes any NSObject* but not just structs. NSMutableArray可以使用任何NSObject *,而不仅仅是结构。

If you're serious about programming in Objective-C, take a look at some tutorials . 如果您对使用Objective-C编程很认真,请看一些教程

Furthermore, NSMutableArrays are meant for convenience, if your adding/deleting a lot of objects to that Array, use plain C-stacks. 此外,NSMutableArrays是为了方便起见,如果您向该Array添加/删除很多对象,请使用普通C堆栈。 Especially for your use-case that more low-level approach will get better performance. 特别是对于您的用例,更底层的方法将获得更好的性能。 Keep in mind, Objective-C(++) is just a superset of C(++), so you can use any C(++) code you are already familiar with. 请记住,Objective-C(++)只是C(++)的超集,因此您可以使用任何您已经熟悉的C(++)代码。

When I wrote my game tactica for iOS, I switched to C-Code whenever I had to do heavy lifting (ie recursive AI-functions that get called hundreds of times per second). 当我为iOS编写游戏策略时,每当需要执行繁重的工作时(例如,每秒被调用数百次的递归AI函数),我都会切换到C代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM