2013-11-02 95 views
1

Obj-C很新,对.h和.m文件的语法非常混淆。我所要做的就是将一个数组传递给我的初始化程序。我得到一个错误在我的.h文件,上面写着Expected a type和说Conflicting parameter types in implementation of initWithColor': '__strong id' vs 'GLKVector4' (aka 'union _GLKVector4')Obj C初始化程序声明

Square.h

#import <Foundation/Foundation.h> 

@interface Square : NSObject 

- (id) initWithColor : (GLKVector4) col; 

@end 

Square.m

#import "Square.h" 
#import <GLKit/GLKit.h> 

#define BG_WIDTH 500.0f 
#define BG_HEIGHT 400.0f 

typedef struct { 
    GLKVector3 positionCoordinates; 
    GLKVector2 textureCoordinates; 
    GLKVector3 normalCoordinates; 
} VertexData; 

VertexData bgRect[] = { 
    { { 0.0f, 0.0f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, }, // 2D - forward facing only 
    { { BG_WIDTH, 0.0f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f} }, 
    { { 0.0f, BG_HEIGHT, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f} }, 
    { { 0.0f, BG_HEIGHT, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f} }, 
    { { BG_WIDTH, 0.0f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f} }, 
    { { BG_WIDTH, BG_HEIGHT, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f} },}; 

@implementation Square { 
    GLKVector4 color; 
} 

- (id) initWithColor : (GLKVector4) col { 
    self = [super init]; 
    if (self) { 
     color = col; 
    } 

    return self; 
} 

@end 

回答

0

#import <GLKit/GLKit.h>移动到Square.h中,因为您在该文件中引用了GLKVector4

1

我会在.m文件警告猜一下。 。

您的代码看起来不错,我认为它只是做以下的事情:

  • 确保您链接到GLKit框架。
  • 进口添加到你的头文件#进口<GLKit/GLKit.h>

enter image description here

无关旁白:

顺便说一句,现代Objective-C的约定,它被认为是强调你的伊娃尔名字的良好做法,如下所示:

@implementation Square { 
    GLKVector4 _color; 
} 

在Xcode或AppCode中,您可以从重构菜单中重命名符号(伊娃,财产等)。

+0

谢谢,但我已经在我的框架中有GLKit。 – Alex

+0

你读过第二点了吗?它说:“将导入添加到您的头文件#import ” –