自定义属性';用c++;objective-c中的结构数组
我有一个c-struct数组,想定义一个属性。 这是相关代码自定义属性';用c++;objective-c中的结构数组,objective-c,ios,properties,Objective C,Ios,Properties,我有一个c-struct数组,想定义一个属性。 这是相关代码 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
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
我第一次这样写是有错误的。
如何使用c-struct数组设置属性,或者自定义setter和getter?
任何提示都将不胜感激 这是我所能接近的
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
希望能有帮助。这是我能得到的最接近的结果
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;
及
相反。不能使用这样的静态数组;malloc()在构造函数和析构函数中分别使用如下内容:
- (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];
}
使用
及
相反。不能使用这样的静态数组;malloc()在构造函数和析构函数中分别使用如下内容:
- (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) 使用NSArray或NSMutableArray保存对象而不是结构 或 2) 将数组放入一个结构中:
typedef struct VertexArray
{
struct Vertex m_vertices [MaxVertices];
};
@property (nonatomic, assign) VertexArray* m_vertices;
或
3) 将数组放入一个对象中
@interface VertexArray
{
struct Vertex m_vertices [MaxVertices];
}
- (struct Vertex)getVertexofIndex:(NSUInteger)index;
- (void)setVertex:(struct Vertex)vertex atIndex:(NSUInteger)index;
@end
对于网格中的属性:
@property (nonatomic, retain) VertexArray* m_vertices;
或者可以将VertexArray的内容直接放在网格中(即成员变量和两个访问器方法)。不能将数组用作属性。你可以做两件事: 1) 使用NSArray或NSMutableArray保存对象而不是结构 或 2) 将数组放入一个结构中:
typedef struct VertexArray
{
struct Vertex m_vertices [MaxVertices];
};
@property (nonatomic, assign) VertexArray* m_vertices;
或
3) 将数组放入一个对象中
@interface VertexArray
{
struct Vertex m_vertices [MaxVertices];
}
- (struct Vertex)getVertexofIndex:(NSUInteger)index;
- (void)setVertex:(struct Vertex)vertex atIndex:(NSUInteger)index;
@end
对于网格中的属性:
@property (nonatomic, retain) VertexArray* m_vertices;
或者,您可以将VertexArray的内容直接放在网格中(即成员变量和两个访问器方法)。在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#>)
这会复制文本字节,速度非常快。它将返回整个数组
实现这一点的更好方法是尽可能创建这样的函数
- (Vertex *)m_vertices_nthIndex:(int)index
{
return(&m_vertices[index]);
}
通过这种方式,您可以获得所需的任何项目的索引。在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#>)
这会复制文本字节,速度非常快。它将返回整个数组
实现这一点的更好方法是尽可能创建这样的函数
- (Vertex *)m_vertices_nthIndex:(int)index
{
return(&m_vertices[index]);
}
这样你就可以得到你需要的任何项目的索引了。< /P> ObjuleC是没有C++的。尽量不要优化那些实际上不必要的东西。Cocoa提供了这么多编写良好的API,一定要使用它们!Objto-C是没有C++的。尽量不要优化那些实际上不必要的东西。Cocoa提供了这么多编写良好的API,一定要使用它们!但是,如何使用类似于m_顶点[i]的东西访问m_顶点中的元素?确切地说,正如
m_顶点[i]
。如果你不知道指针可以像数组一样被下标,那么你真的应该阅读一些C指南。但是,如何使用类似于m_顶点[i]的东西访问m_顶点中的元素呢?确切地说,就像m_顶点[i]
。如果您不知道指针可以像数组一样被订阅,那么您真的应该阅读一些C指南。