2009-08-07 78 views
30

每當我構建下面的代碼,我得到上面的錯誤。錯誤:在目標C之前的預期說明符 - 限定符列表?

//Controller.h 
#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 
#import "PolygonShape.h" 
#import "PolygonView.h"; 

@interface Controller : NSObject 
{ 
    IBOutlet UIButton *decreaseButton; 
    IBOutlet UIButton *increaseButton; 
    IBOutlet UILabel *numberOfSidesLabel; 
    IBOutlet PolygonShape *shape; 
    IBOutlet PolygonView *shapeView; 
} 
- (IBAction)decrease; 
- (IBAction)increase; 
- (void)awakeFromNib; 
@end 


//Controller.m 
#import "Controller.h" 


@implementation Controller 
@end  

但是,當我替換導入語句,並將一個轉發類引用,而不是代碼編譯。

//Controller.h 

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 
#import "PolygonShape.h" 
@class PolygonView; 

@interface Controller : NSObject 
{ 
    IBOutlet UIButton *decreaseButton; 
    IBOutlet UIButton *increaseButton; 
    IBOutlet UILabel *numberOfSidesLabel; 
    IBOutlet PolygonShape *shape; 
    IBOutlet PolygonView *shapeView; 
} 
- (IBAction)decrease; 
- (IBAction)increase; 
- (void)awakeFromNib; 
@end 

//Controller.m 
#import "Controller.h" 
#import "PolygonView.h" 

@implementation Controller 
@end 

任何人都可以解釋一下嗎?

+5

PolygonShape和PolygonView可能具有周期性導入。這就是前瞻性聲明存在的原因。請使用它們。 – 2009-08-07 19:19:29

+0

PolygonShape和PolygonView沒有循環導入,但是PolygonView和Controller沒有。謝謝您的幫助! – Ridwan 2009-08-07 19:25:26

+2

@Jason:這可能會是一個很好的答案。 – Chuck 2009-08-07 19:25:27

回答

3

發生這種情況是因爲您忘記了包含其中一個頭文件。我也遇到了同樣的錯誤,但在正確包含頭文件後,錯誤消失了。

8

我得到這個錯誤,一個簡單的錯誤......也許別人也這樣做?

正確的:

@interface ButtonDevice : NSObject 

{ 

    NSString *name; 
    NSString *uri; 
    NSString *icon; 
} 

    @property (nonatomic, retain) IBOutlet NSString *name; 
    @property (nonatomic, retain) IBOutlet NSString *uri; 
    @property (nonatomic, retain) IBOutlet NSString *icon; 
@end 

WRONG:

@interface ButtonDevice : NSObject 
{ 

    NSString *name; 
    NSString *uri; 
    NSString *icon; 

    @property (nonatomic, retain) IBOutlet NSString *name; 
    @property (nonatomic, retain) IBOutlet NSString *uri; 
    @property (nonatomic, retain) IBOutlet NSString *icon; 
} 
@end 
+0

這兩個不同?我沒看到它。 – 2011-10-07 13:27:10

+0

屬性之前的支架與 – LordFire 2011-10-12 11:14:57

+0

之後的支架無用..它的寫法是一樣的「你應該在@end前鍵入@implementation」這很明顯,你應該只將類變量放在花括號中 – Stas 2012-04-10 08:15:54

3

只是還包括一類似乎被忽視:

#import "NameOfClass.h" 

,並進行了排序。謝謝。

2

您還可以檢查是否有拼寫錯誤。例如,我有一個名爲「MyUIViewController」的類,但我寫道:

@property (nonatomic, retain) MyViewController *myViewController; 

並且出現此錯誤。之後我將其更正爲:

@property (nonatomic, retain) MyUIViewController *myViewController; 

錯誤已修復,計算機是如此的字面意思。

1

我剛剛在一個Objective-C++項目中遇到了這個問題 - 在這種情況下,如果忘記命名實現文件.mm而不是.m,則會出現一個錯誤。

6

只要按照此規則,以避免這樣的問題:

If an object is only used internally by class's implementation file, use forward declaration in the header file and import/include in the implementation file. Otherwise use import in the header.

+0

請參考? – Madbreaks 2012-12-12 21:46:07

0

如果刪除了預編譯前綴頭,你必須手動導入需要到該文件的標題,文件:

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 
#import <CoreData/CoreData.h> 
1

我試圖引用我自己設置的協議時遇到了此錯誤。

這顯示了我的協議,錯誤的方法以及我認爲正確的方法。

in .h 
// ------------------------------------ EtEmailDelegate Protocol -BEGIN- -------------------------------------- 
@protocol EtEmailDelegate 
    - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error; //<-- This is really just one chunk from the MFMailComposeViewControllerDelegate 
@end 
// ------------------------------------ EtEmailDelegate Protocol -END- -------------------------------------- 


@interface ClsEtEmail : NSObject < MFMailComposeViewControllerDelegate> { 
    // owner 
    UIViewController *myUivc; 
    //EtEmailDelegate *myDelegate; // <--- the wrong way, throw error 
    id<EtEmailDelegate> *myDelegate; // <-- the right way (i think) 
    ... 
    } 
    @property (nonatomic, readwrite, assign) id<EtEmailDelegate> delegate; 
@end 

只是爲了完整性,這裏是我如何實現一些方法也依賴於協議...

in .m 
@synthesize delegate = myDelegate; 

// my static initializer 
+(id) objEtEmailWithUivc: (UIViewController*) theUivc delegate: (id <EtEmailDelegate>) theDelegate { 
    ClsEtEmail * obj = [[[ClsEtEmail alloc] initWithlUivc: theUivc delegate:theDelegate] autorelease];  
    return obj; 
} 


// my normal init 
- (id)initWithlUivc: (UIViewController*) theUivc delegate: (id <EtEmailDelegate>) theDelegate { 
    self = [super init]; 
    if (self) { 
    [self init_]; // my private init (not seen) 
    self.uivc = theUivc; 
    NSAssert([theDelegate conformsToProtocol:@protocol(EtEmailDelegate)],@"whoh - this can't is notE tEmailDelegate"); 
    self.delegate = theDelegate; 
    } 
    return self; 
} 

希望這可以幫助其他人。

相關問題