English 中文(简体)
自定义属性 目标-c中带有 c+++ 结构阵列的获取和设置器
原标题:Custom property s getter and setter with c++ struct array in objective-c

I ve got an c-struct array, and want to define a property. Here is the relative code..

struct Vertex {
    float x, y, z;
    float nx, ny, nz;
    float u, v;
};

@interface Mesh : NSObject{
    Vertex m_vertices[MaxVertices];
}
@property (nonatomic) Vertex m_vertices[MaxVertices];
@end

@implementation Mesh;
@synthesize m_vertices[MaxVertices];
@end

I first wrote like this with an error. how to set the property with c-struct array, or customlize the setter and getter? Any tips will be appreciated!

最佳回答

我离这里很近

typedef struct _Vertex {
float x, y, z;
float nx, ny, nz;
float u, v;
} Vertex;

#define MaxVertices 5

@interface Mesh : NSObject{
    Vertex m_verticesX[MaxVertices];
    Vertex *m_vertices;
}
@property (nonatomic) Vertex *m_vertices;
@end

@implementation Mesh;
@synthesize m_vertices;
- (Vertex *)m_vertices
{
    if (!m_vertices)
        m_vertices = m_verticesX;
    return m_vertices;
}
@end

希望能帮上忙

问题回答

使用使用

@property (nonatomic) Vertex *m_vertices;

@synthesize m_vertices;

instead. You can t use a static array like this; malloc() it using something like this in your constructor 和 free() in the destructor:

- (id)init
{
    if ((self = [super init]))
    {
        m_vertices = malloc(sizeof(*m_vertices) * NUM_OF_VERTICES);
    }
    return self;
}

- (oneway void)dealloc
{
    free(m_vertices);
    [super dealloc];
}

您不能将数组用作属性。 您可以做两件事 :

(1) 使用非国家行为者或NSMubableAray来屏蔽物体而不是支架。

(2) 将数组置于结构中:

typedef struct VertexArray
{
    struct Vertex m_vertices [MaxVertices];
};

@property (nonatomic, assign) VertexArray* m_vertices;

(3) (3) 将数组放入对象

@interface VertexArray
{
    struct Vertex m_vertices [MaxVertices];
}

- (struct Vertex)getVertexofIndex:(NSUInteger)index;
- (void)setVertex:(struct Vertex)vertex atIndex:(NSUInteger)index;

@end

and f或 the property in Mesh:

@property (nonatomic, retain) VertexArray* m_vertices;

或 you can put the contents of VertexArray directly within Mesh (i.e. the member variable and two access或 methods).

当返回 C 中的数组( 任何类型) 时,您要返回数组的第一个索引。

所以,如果我想返回下面的变量, 在接口中声明的变量。

 Vertex m_vertices[MaxVertices];

我可以说...

- (Vertex *)m_vertices
{
    return m_vertices;
}

上面说的和上面说的一模一样...

- (Vertex *)m_vertices
   {
       return &m_vertices[0];
   }

如果您想要返回整个阵列, 这样做的最好方法可能是使用Memcpy指令。

memcpy(<#void *#>, <#const void *#>, <#size_t#>)

此处参引:http://www.cplusplus.com/reference/cstring/memcpy/

像这样写入函数...

- (Vertex *)m_vertices
{
   Vertex *localVertex;
   memcpy(localVertex,&m_vertices,sizeof(Vertex)* MaxVertices);
   return localVertex;
}

这复制了字面字节, 并且非常快。 它会返回整个阵列 。

这样做的最好方式是使这种功能也有可能发挥。

- (Vertex *)m_vertices_nthIndex:(int)index
{
   return(&m_vertices[index]);
}

这样你就可以得到你需要的东西的索引了





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签