2010-07-06 141 views
2

我有一個方法:我在這裏做了什麼愚蠢的noob錯誤?

- (CGPoint) calculateVectorBetweenThisPoint:(CGPoint)firstPoint andThisPoint:(CGPoint)secondPoint 
{ 
    float xDif = firstPoint.x - secondPoint.x; 
    float yDif = firstPoint.y - secondPoint.y; 

    return CGPointMake(xDif, yDif); 
} 

我嘗試如下調用它:

- (float) angleBetweenThisPoint:(CGPoint)firstPoint andThisPoint:(CGPoint)secondPoint 
{ 
// COMPILE ERROR HERE: Invalid Initializer 
    CGPoint vector = [self calculateVectorBetweenThisPoint:firstPoint andThisPoint:secondPoint]; 

    return atan2(vector.x, vector.y); 
} 

但我得到一個編譯erorr,我嘗試使用方法:「無效的初始化程序」。

我在做什麼錯?

回答

5

calculateVectorBetweenThisPoint:andThisPoint:是否在使用前聲明?如果不是,編譯器將假定返回類型爲id,這對於放入CGPoint絕對是無效的。

+0

正確。 'calculateVectorBetweenThisPoint:andThisPoint:'必須在頭文件中定義,或者放置在實現文件中的'angleBetweenThisPoint:andThisPoint:'之前。 – 2010-07-06 11:24:01

+0

+1(即使你的意思是聲明,沒有定義) – 2010-07-06 11:38:10

+0

哎呀,你是對的。編輯! – Wevah 2010-07-06 11:41:57