2011-05-25 25 views
2

我收到錯誤時「currentUpdateMethod」未申報(第一本函數中使用)。這個引用的變量currentUpdateMethod是頭文件中聲明的SEL類型的實例變量。因爲構建到模擬器並運行應用程序的工作如期,我相信我已經正確設置了一切。這個錯誤只在今天出現 - 我已經在設備上測試了幾天而沒有問題。我確實試圖清理和清理所有目標。我甚至在xcode中輸入變量名稱到文件中,併爲我自動完成變量。什麼可能導致設備編譯失敗這些變量,但不是模擬器的編譯?變量「未申報」只編譯爲設備

編輯:代碼如下。

超類:

#import "Deployable.h" 

@interface Drawable : Deployable { 

float currentDelta; 

SEL currentUpdateMethod; 
SEL currentAnimationMethod; 
SEL currentBatchMethod; 

float rotation; 
} 

- (id) init; 

- (id) initWithActivationTime:(float)time; 

- (int) updateWithDelta:(float)delta; 

- (int) animate; 

- (int) batch; 

@end 

那麼問題類:

#import "Drawable.h" 
#import "Structures.h" //contains Vector2f declaration 

@interface Player : Drawable { 

    Image *playerGraphic; 

    Vector2f position; 

} 

@property (nonatomic) Vector2f position; 

- (id) initWithImage:(Image *)aGraphic andPosition:(Vector2f)aPosition; 

- (void) setupInactiveState; 
- (int) updateInactiveState; 
- (int) animateInactiveState; 
- (int) batchInactiveState; 

- (void) setupActiveState; 
- (int) updateActiveState; 
- (int) animateActiveState; 
- (int) batchActiveState; 

@end 

而且它的實施中,在錯誤拋出:

#import "Player.h" 
#import "AIEngine.h" 

@implementation Player 

@synthesize position; 

- (id) initWithImage:(Image *)aGraphic andPosition:(Vector2f)aPosition { 

    self = [super init]; 

    if(self) { 

     playerGraphic = [aGraphic retain]; 
     position = aPosition; 


    } 

    return self; 
} 

- (int) deployWithScene:(MainScene *)newScene { 

    [super deployWithScene:newScene]; 

    [self setupInactiveState]; 

    return 1; 
} 

- (void) setupInactiveState { 

    currentUpdateMethod = @selector(updateInactiveState); //'currentUpdateMethod' undeclared (first use in this function) 
    currentAnimationMethod = @selector(animateInactiveState); //'currentAnimateMethod' undeclared (first use in this function) 
    currentBatchMethod = @selector(batchInactiveState); //'currentAnimateMethod' undeclared (first use in this function) 

} 

- (void) setupActiveState {  

    currentUpdateMethod = @selector(updateActiveState); //'currentUpdateMethod' undeclared (first use in this function) 
    currentAnimationMethod = @selector(animateActiveState); //'currentAnimateMethod' undeclared (first use in this function) 
    currentBatchMethod = @selector(batchActiveState); //'currentBatchMethod' undeclared (first use in this function) 

} 

@end 

只是重申一下,這六個錯誤只有建築設備時拋出。當我爲模擬器構建時,應用程序會生成並正常運行。

EDIT2:我切換到僅LLVM和誤差不拋出。我想找出問題的根源,而不是僅僅使用其他編譯器。有任何想法嗎?

+2

你能告訴我們代碼嗎?你可能在某個地方有一些'#ifdef'宏,這可能會讓事情變得糟糕。 – BoltClock 2011-05-25 05:17:37

+0

@BoltClock你真的認爲他會知道如何放置這樣的宏 - 但沒有意識到這是造成問題的原因?但我同意沒有代碼就沒有太多的事情可以做... – 2011-05-25 05:29:14

+0

您使用的是GCC還是LLVM?海灣合作委員會有一些...有趣的錯誤與'受保護的'變量相關(見http://stackoverflow.com/questions/2778405/not-so-silly-objective-c-inheritance-problem-when-using-property-gcc- bug) – jv42 2011-05-25 13:09:25

回答

2

我敢肯定,這是在Xcode中的錯誤,因爲沒有什麼不對您的代碼,我可以看到。

我會嘗試這兩個東西:

1)快速修復,你可以嘗試合成的變量,然後用

[self setCurrentUpdateMethod:@selector(updateInactiveState)]; 

2更換

currentUpdateMethod = @selector(updateInactiveState); 

)拆下來自您項目的文件。從頭開始重新創建班級。將舊代碼複製到新文件中。

貌似這傢伙也有類似的問題:Strange error regarding instance variables & superclass

讓我知道,如果這些建議幫助。如果他們這樣做,我會懇請您到bug提交給開發者的Xcode :)

+0

這解決了它!非常奇怪... – brodney 2011-05-27 12:37:32

2

我有同樣的問題幾個星期

"Variable Undeclared" error when compiling to iOS Device, but not for Simulator

一個解決方案,我發現是簡單地改變編譯器從默認LLVM GCC 4.2LLVM Compiler 2.0(或Apple LLVM Compiler 2.1)。這似乎是編譯器中的一個錯誤,但這只是一個猜測。

改變它是你的問題速戰速決,如果沒有必要爲你使用GCC編譯器在所有。

+0

它似乎是一個編譯器錯誤。在我的帖子結尾處,我提到了切換到LLVM,錯誤消失了。 – brodney 2011-08-04 19:12:10

+0

對不起,還沒有看到編輯...所以你的問題和我的一模一樣 – 2011-08-04 23:46:37

+0

的確如此。正如3rdFunkyBot建議的那樣,通過消息傳遞顯式設置變量確實解決了「錯誤」問題。 – brodney 2011-08-06 00:52:15