2009-12-13 135 views
1

我在Xcode中得到一個錯誤,提示「whatEverVariable未聲明」這是在.m文件中發生的,也是類本身之外的情況。我已經在.h文件的大括號中聲明瞭變量,並使用@property和@synthesize來訪問該變量。不過,我有時會在.m文件中得到上述錯誤,甚至在其他類中我已經#imported包含該變量的類。我在這裏還是缺少一些有關Objective-C的內容?我想我所問的是,爲什麼如果宣佈這些事情就不會宣佈。爲什麼編譯器說我沒有聲明我的變量?

而且我得到「按鈕可能不響應‘的setTitle’的,以及有這種被棄用錯誤或某事‘的setTitle:forState:’因爲我不想使用

我會發表一個例子,但我真的認爲我錯過了一些明顯的事情在這裏。有沒有人有什麼想法會導致錯誤?對不起,這是一般的,但我必須是一個白癡或什麼?

這是說「picturesInArray未申報」

#import <Foundation/Foundation.h> 
@interface Pictures : NSObject { 
    NSMutableArray *picturesInArray; 
} 

@property (nonatomic, retain) NSMutableArray *picturesInArray; 

-(IBAction)pictureButtonPressed:(id)sender; 

@end 


#import "Pictures.h" 
@implementation Pictures 

@synthesize picturesInArray; 

-(IBAction)pictureButtonPressed:(id)sender { 

    if (pictureIndex >= [picturesInArray count] || pictureIndex < 0) { 
     pictureIndex = 0;  

     [imageView setImage:[picturesInArray objectAtIndex:pictureIndex]]; 

     [imageView setNeedsDisplay]; 
    }  
} 
+1

我相信你缺少一些明顯的東西,但很難準確地分辨出你在沒有看到任何代碼的情況下犯的錯誤。 – Chuck 2009-12-13 19:50:57

+1

您還應該在標題中發佈問題摘要。你能想象登錄到SO,頭版有50條條目都會說「我有問題」。 – 2009-12-13 20:01:26

+0

您必須在實現塊的末尾錯過@end。顯然,你並沒有顯示整個代碼。可以爲我們提供這個課程的完整代碼嗎?否則,剝離所有冗餘變量的當前樣本,並嘗試僅在定義了picturesInArray的情況下對其進行編譯。 – 2009-12-13 21:59:20

回答

-1

這話說「picturesInArray是未申報」很抱歉把它放進「上述評論它使人們難以閱讀!

#import <Foundation/Foundation.h> 
@interface Pictures : NSObject { 
    NSMutableArray *picturesInArray; 
} 

@property (nonatomic, retain) NSMutableArray *picturesInArray; 

-(IBAction)pictureButtonPressed:(id)sender; 

@end 


#import "Pictures.h" 
@implementation Pictures 

@synthesize picturesInArray; 

-(IBAction)pictureButtonPressed:(id)sender { 

    if (pictureIndex >= [picturesInArray count] || pictureIndex < 0) { 
     pictureIndex = 0;  

     [imageView setImage:[picturesInArray objectAtIndex:pictureIndex]]; 

     [imageView setNeedsDisplay]; 
    }  
} 
+0

這對我來說很好。如果我添加「int pictureIndex; NSImageView * imageView;」到.h文件,這對我來說沒有任何錯誤或警告。你使用什麼編譯器? – 2009-12-13 20:57:52

0

你能發佈示例代碼嗎?

我的猜測是,你沒有聲明whatEverVariable在某個時刻的對象/變量類型。確保你的頭看起來像這樣:

MyClass.h:

@interface MyClass : NSObject 
{ 
    NSString* whatEverVariable; 
} 

@property(readwrite, assign) NSString* whatEverVariable; 

@end 

MyClass.m:

@implementation MyClass 

//whatever methods 

@synthesize whatEverVariable; 

@end 

請注意,在你的@property你再次聲明變量的類型。

至於按鈕,請確保它是NSButton類型。

另外,你有進口嗎?這爲NSButton之類的東西導入了很多頭文件。

+0

這是說「picturesInArray未申報」 #import <基金會/基金會。h> @interface圖片:NSObject { \t NSMutableArray * picturesInArray; } @property(nonatomic,retain)NSMutableArray * picturesInArray; - (IBAction)pictureButtonPressed:(id)sender; @end #import「Pictures.h」 @implementation圖片 @synthesize picturesInArray; (IBAction)pictureButtonPressed:(id)sender if(pictureIndex> = [picturesInArray count] || pictureIndex <0) { pictureIndex = 0; \t [imageView setImage:[picturesInArray objectAtIndex:pictureIndex]]; [imageView setNeedsDisplay]; } \t } – I00I 2009-12-13 20:22:26

2

這話說」 picturesInArray是未申報」

你確定它不是說「pictureIndex未申報」嗎?因爲這是你所展示的代碼中未申報的變量。

相關問題