期待“;”之前{令牌

时间:2011-10-19 09:24:50

标签: objective-c ios

  

可能重复:
  Expected “;” before “{” token?

所以我在从一本书中复制的一段代码上得到了错误 在接口定义之后,定义了重写方法。这是我似乎遇到问题的地方。试图让这段代码工作,这样我就可以继续学习了。

#import <Foundation/Foundation.h>

@interface Sprite : NSObject{
    CGFloat x; 
    CGFloat y;
    CGFloat r;
    CGFloat g;
    CGFloat b;
    CGFloat alpha; //alpha value, for transparency
    CGFloat speed; //speed of movement in pixels/frame
    CGFloat angle; //angle of movement in degrees
    CGFloat rotation; //rotation of our sprite in degrees about the center
    CGFloat width;
    CGFloat height;
    CGFloat scale; //uniform scaling factor for size
    int frame; //for animation

    CGFloat cosTheta; //precomputed for speed
    CGFloat sinTheta;
    CGRect box; //our bounding box

    BOOL render; //true when we're rendering
    BOOL offScreen; //true when we're off the screen
    BOOL wrap; //true if you want the motion to wrap the screen
}

@property (assign) BOOL wrap, render, offScreen;
@property (assign) CGFloat x, y, r, g, b, alpha;
@property (assign) CGFloat speed, angle, rotation;
@property (assign) CGFloat width, height, scale;
@property (assign) CGRect box;
@property (assign) int frame; 

// THIS IS WHERE I'M GETTING THE ERROR
- (void) setRotation: (CGFloat) degrees 
{
    rotation = degrees * 3.141592 / 180.0;
}

-(CGFloat) rotation
{
    return rotation*180.0/3.141592;
}

- (void) setAngle: (CGFloat) degrees
{
    angle = degrees*3.141592/180.0;
    cosTheta = cos(angle);
    sinTheta = sin(angle);
}

- (CGFloat) angle
{ 
    return  angle*180.0/3.141592; 
}

@end

4 个答案:

答案 0 :(得分:2)

这都在同一个文件中吗?您应该只在.h文件中声明方法签名,并在实现部分中详细说明。

所以,你的.h文件将是原样,除了方法细节,你只需用分号结束每个签名:

- (void) setRotation: (CGFloat) degrees; 
- (CGFloat) rotation;
- (void) setAngle: (CGFloat) degrees;
- (CGFloat) angle;

并将实际实现放在.m文件中。

答案 1 :(得分:1)

接口只包含方法的原型,而不包含其内容。

您需要单独的@implementation

答案 2 :(得分:1)

你在.h文件中缺少@end,这就是你收到预期的原因;在令牌之前

此外,如果你没有在.h文件中声明方法,那么它们对那个类是私有的。

所以用这个内容创建.h文件

#import <Foundation/Foundation.h>

@interface Sprite : NSObject{
    CGFloat x; 
    CGFloat y;
    CGFloat r;
    CGFloat g;
    CGFloat b;
    CGFloat alpha; //alpha value, for transparency
    CGFloat speed; //speed of movement in pixels/frame
    CGFloat angle; //angle of movement in degrees
    CGFloat rotation; //rotation of our sprite in degrees about the center
    CGFloat width;
    CGFloat height;
    CGFloat scale; //uniform scaling factor for size
    int frame; //for animation

    CGFloat cosTheta; //precomputed for speed
    CGFloat sinTheta;
    CGRect box; //our bounding box

    BOOL render; //true when we're rendering
    BOOL offScreen; //true when we're off the screen
    BOOL wrap; //true if you want the motion to wrap the screen
}

@property (assign) BOOL wrap, render, offScreen;
@property (assign) CGFloat x, y, r, g, b, alpha;
@property (assign) CGFloat speed, angle, rotation;
@property (assign) CGFloat width, height, scale;
@property (assign) CGRect box;
@property (assign) int frame; 

- (void) setRotation: (CGFloat) degrees; 
- (CGFloat) rotation;
- (void) setAngle: (CGFloat) degrees;
- (CGFloat) angle;

 @end

然后在.m文件中

#import "Sprite.h"

@implementation Sprite

- (void) setRotation: (CGFloat) degrees 
{
    rotation = degrees * 3.141592 / 180.0;
}

-(CGFloat) rotation
{
    return rotation*180.0/3.141592;
}
- (void) setAngle: (CGFloat) degrees
{
    angle = degrees*3.141592/180.0;
    cosTheta = cos(angle);
    sinTheta = sin(angle);
}

- (CGFloat) angle
{ 
    return  angle*180.0/3.141592; 
}

@end

答案 3 :(得分:-3)

只是一个猜测,但尝试像这样放一个分号:

BOOL render; //true when we're rendering
BOOL offScreen; //true when we're off the screen
BOOL wrap; //true if you want the motion to wrap the screen
};