简体   繁体   English

Objective-C自定义类

[英]Objective-C custom class

I get the error: 我得到错误:

VertexPoint.m:12:1: Type of property 'color' ('GLfloat *' (aka 'float *')) does not match type of instance variable '_color' ('float [4]') VertexPoint.m:12:1:属性'color'的类型('GLfloat *'(aka'float *'))与实例变量'_color'('float [4]')的类型不匹配

and

VertexPoint.m:12:1: Type of property 'position' ('GLfloat *' (aka 'float *')) does not match type of instance variable '_position' ('float [3]') VertexPoint.m:12:1:属性“位置”的类型(“ GLfloat *”(又名“ float *”))与实例变量“ _position”(“ float [3]”)的类型不匹配

What should i do to resolve it? 我应该怎么解决呢?

Here is the code: 这是代码:

//
//  VertexPoint.m

#import <Foundation/Foundation.h>
#import "VertexPoint.h"

@implementation VertexPoint

- (id)init {
     self = [super init];
    if (self) {

        _position[0] = 1;
        _position[1] = 2;
        _position[2] = 3;

        _color[0] = 1;
        _color[1] = 2;
        _color[2] = 3;
        _color[3] = 4;
    }
    return self;

};

- (GLfloat *)x {
    return &_position[0];
};
- (GLfloat *)y {
    return &_position[1];
};
- (GLfloat *)z {
    return &_position[2];
};
- (GLfloat *)r {
    return &_color[0];
};
- (GLfloat *)g {
    return &_color[1];
};
- (GLfloat *)b {
    return &_color[2];
};
- (GLfloat *)a {
    return &_color[3];
};

- (void)setPosition:(GLfloat *)pos {
    for (NSUInteger i = 0; i < 3; i++)
        _position[i] = pos[i];
};
- (void)setColor:(GLfloat *)col{
    for (NSUInteger i = 0; i < 4; i++)
        _color[i] = col[i];
};

@end

and

//  VertexPoint.h
//


#import <Foundation/Foundation.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>

@interface VertexPoint : NSObject
{
    GLfloat _position[3];
    GLfloat _color[4];
}
@property(nonatomic,readwrite) GLfloat *position;
@property(nonatomic,readwrite) GLfloat *color;
@property(nonatomic,readwrite) GLfloat *x;
@property(nonatomic,readwrite) GLfloat *y;
@property(nonatomic,readwrite) GLfloat *z;
@property(nonatomic,readwrite) GLfloat *r;
@property(nonatomic,readwrite) GLfloat *g;
@property(nonatomic,readwrite) GLfloat *b;
@property(nonatomic,readwrite) GLfloat *a;

- (GLfloat *)x;
- (GLfloat *)y;
- (GLfloat *)z;
- (GLfloat *)r;
- (GLfloat *)g;
- (GLfloat *)b;
- (GLfloat *)a;
-(void)setColor:(GLfloat *)col;
-(void)setPosition:(GLfloat *)pos;
@end

UPDATED AGAIN (2) 重新更新(2)

The new h and m files: 新的h和m文件:

#import <Foundation/Foundation.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>

@interface VertexPoint : NSObject
{
    GLfloat _position[3];
    GLfloat _color[4];
}
@property(nonatomic,readwrite) GLfloat *position;
@property(nonatomic,readwrite) GLfloat *color;
@property(nonatomic,readonly) GLfloat x;
@property(nonatomic,readonly) GLfloat y;
@property(nonatomic,readonly) GLfloat z;
@property(nonatomic,readonly) GLfloat r;
@property(nonatomic,readonly) GLfloat g;
@property(nonatomic,readonly) GLfloat b;
@property(nonatomic,readonly) GLfloat a;

- (GLfloat)x;
- (GLfloat)y;
- (GLfloat)z;
- (GLfloat)r;
- (GLfloat)g;
- (GLfloat)b;
- (GLfloat)a;
-(void)setColor:(GLfloat *)col;
-(void)setPosition:(GLfloat *)pos;
@end

and

#import <Foundation/Foundation.h>
#import "VertexPoint.h"

@implementation VertexPoint

- (id)init {
     self = [super init];
    if (self) {

        _position[0] = 1.0f;
        _position[1] = 1.0f;
        _position[2] = 1.0f;

        _color[0] = 1.0f;
        _color[1] = 1.0f;
        _color[2] = 1.0f;
        _color[3] = 1.0f;
    }
    return self;

};

- (GLfloat)x {
    return _position[0];
};
- (GLfloat)y {
    return _position[1];
};
- (GLfloat)z {
    return _position[2];
};
- (GLfloat)r {
    return _color[0];
};
- (GLfloat )g {
    return _color[1];
};
- (GLfloat )b {
    return _color[2];
};
- (GLfloat )a {
    return _color[3];
};

- (void)setPosition:(GLfloat *)pos {
    for (NSUInteger i = 0; i < 3; i++)
        _position[i] = pos[i];
};
- (void)setColor:(GLfloat *)col{
    for (NSUInteger i = 0; i < 4; i++)
        _color[i] = col[i];
};

@end

The original error message is still there. 原始错误消息仍然存在。

Add: 加:

- (GLfloat *)color {
    return &_color[0];
}

- (GLfloat *)position {
    return &_position[0];
}

to your .m implementation file. 到您的.m实施文件。

What's happening is that you've declared properties for color and position with types GLfloat * , and you have instance variables called _color and _position . 发生的事情是,您已经使用GLfloat *类型声明了colorposition属性,并且有名为_color_position实例变量。 The compiler wants to link these together automatically through the standard property-ivar naming convention, but these mismatch in types (the compiler is complaining because a float pointer and an array of floats aren't the same thing for these purposes; you can't return an array in C.) 编译器希望通过标准的属性-ivar命名约定将它们自动链接在一起,但是这些类型不匹配(编译器抱怨是因为在这些目的上,浮点指针和浮点数组不是同一件事;您不能在C中返回一个数组。)

If you explicitly add getter methods that correspond to the properties, then you can provide the mapping (and different typing) explicitly yourself, as I've shown here. 如果您显式添加与属性相对应的getter方法,则可以自己显式提供映射(和其他类型),如我在此处所示。 In these two cases, I've returned simply the pointer to the first element in the array. 在这两种情况下,我仅返回了指向数组中第一个元素的指针。 The caller in these cases will have to simply know that the color array has four elements and the position one has three. 在这种情况下,调用方将只需要简单地知道颜色数组包含四个元素,位置一个包含三个元素。

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

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