通过 Objective-C 消息传递访问 C 风格对象数组的正确语法?
请参阅评论:
static void drawAnObject() {
Form *form = [[Form alloc] init];
int i;
[form randomizeCube];
glColor3f(0.0, 0.0, 0.0);
for(i = 0; i < MAX_CUBE; i++) {
glutWireCube(form->cube[i]->size);
//compiler issues hard warning because form->cube is protected!!
}
}
我宁愿使用放置在“Form”类上的访问器,所以我宁愿编写类似的内容:
glutWireCube([[form cube][i] size]);
但是当我尝试编译它时,我收到“无法转换为指针类型”错误。
这是我在“Form”类中的访问器:
@implementation Form
- (Cube *) cube {
return *cube;
}
变量“cube”在“Form”类的头文件中定义如下:
@interface Form : NSObject <NSCopying> {
Cube *cube[MAX_CUBE];
}
以下是“Cube”类的头文件中定义的变量:
@interface Cube : NSObject <NSCopying> {
double size;
double positionX;
double positionY;
double positionZ;
}
...以及实现 (.m) 文件中的相应访问器:
- (double) size {
return size;
}
- (void) setSize: (double) newSize {
size = newSize;
}
- (double) positionX {
return positionX;
}
- (void) setPositionX: (double) newPositionX {
positionX = newPositionX;
}
- (double) positionY {
return positionY;
}
- (void) setPositionY: (double) newPositionY {
positionY = newPositionY;
}
- (double) positionZ {
return positionZ;
}
- (void) setPositionZ: (double) newPositionZ {
positionZ = newPositionZ;
}
我想避免使用 NSMutableArray o NSArray,因为我计划稍后将项目代码的这一部分移植到非 Cocoa 平台。
在过去的几个小时里我一直在寻找正确的方法来做到这一点。是否有“正确的方法”使用 C 样式数组和 Objective-C 访问器来做到这一点?
Please see comment:
static void drawAnObject() {
Form *form = [[Form alloc] init];
int i;
[form randomizeCube];
glColor3f(0.0, 0.0, 0.0);
for(i = 0; i < MAX_CUBE; i++) {
glutWireCube(form->cube[i]->size);
//compiler issues hard warning because form->cube is protected!!
}
}
I would rather use the accessor I placed on the "Form" class so I could rather write something like:
glutWireCube([[form cube][i] size]);
But I get a "cannot convert to a pointer type" error when I attempt to compile this.
This is my accessor in the "Form" class:
@implementation Form
- (Cube *) cube {
return *cube;
}
The variable "cube" is defined in the "Form" class' header file as follows:
@interface Form : NSObject <NSCopying> {
Cube *cube[MAX_CUBE];
}
The following are the "Cube" class' variables as defined in its header file:
@interface Cube : NSObject <NSCopying> {
double size;
double positionX;
double positionY;
double positionZ;
}
...and the corresponding accessors in the implementation (.m) file:
- (double) size {
return size;
}
- (void) setSize: (double) newSize {
size = newSize;
}
- (double) positionX {
return positionX;
}
- (void) setPositionX: (double) newPositionX {
positionX = newPositionX;
}
- (double) positionY {
return positionY;
}
- (void) setPositionY: (double) newPositionY {
positionY = newPositionY;
}
- (double) positionZ {
return positionZ;
}
- (void) setPositionZ: (double) newPositionZ {
positionZ = newPositionZ;
}
I would like to avoid using an NSMutableArray o NSArray as I plan to port this part of the project's code to non-Cocoa platforms later on.
I've spent the last couple of hours searching for the correct way to do this. Is there "a right way" to do this using C-Style arrays and Objective-C accessors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的访问器方法中缺少一个星号。这很棘手,因为您无法从 C 或 Objective-C 中的方法返回数组类型。试试这个:
这会返回一个指向指针的指针,因此当您使用
[formcube][i]
对其进行下标时,您仍然保留指针类型,并且在 Objective-C 接收器中必须是指针类型。这应该允许你做You're missing an asterisk in your accessor method. It is tricky because you cannot return array types from methods in C or Objective-C. Try this:
This returns a pointer to a pointer, so when you subscript it with
[form cube][i]
you are still left with a pointer type, and in Objective-C receivers must be pointer types. This should allow you to do