objective-c 错误:预期的说明符限定符列表之前...在目标 C 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1246509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:02:46  来源:igfitidea点击:

error: expected specifier-qualifier-list before...in Objective C?

iphoneobjective-c

提问by Ridwan

Whenever I build the following code, I get the error above.

每当我构建以下代码时,都会出现上述错误。

//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    

However, when I replace the import statement and put a forward class reference instead, the code compiles.

但是,当我替换 import 语句并放置一个前向类引用时,代码会编译。

//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

Can anyone explain?

谁能解释一下?

采纳答案by ChrisJF

Yes, I too had this problem of Cyclical Dependencies where I was importing both classes in each other. I also didn't know what Forward Declarations were. I promptly searched it on Wikipedia but it was poorly described. I found this post that explains what they are and how they relate to cyclical imports. http://timburrell.net/blog/2008-11-23/effective-c-cyclical-dependencies/

是的,我也遇到了循环依赖的问题,我在其中相互导入了两个类。我也不知道前向声明是什么。我立即在维基百科上搜索了它,但它的描述很差。我发现这篇文章解释了它们是什么以及它们与周期性进口的关系。http://timburrell.net/blog/2008-11-23/effective-c-cyclical-dependencies/

Note: The link keeps going down so I just PDF'd it.

注意:链接不断下降,所以我只是将其 PDF 化

回答by Dan Brickley

I got this error with a simple mistake... perhaps others are doing likewise?

我犯了一个简单的错误……也许其他人也在做同样的事情?

CORRECT:

正确的:

@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

回答by Kentzo

Just follow this rule to avoid such problems:

只需遵循此规则即可避免此类问题:

If an object is only used internally by class's implementation file, use forward declarationin the header file and import/includein the implementation file. Otherwise use importin the header.

如果对象仅在类的实现文件内部使用,则在头文件中使用前向声明并在实现文件中使用 导入/包含。否则在标题中使用 导入

回答by meet bhatha

This happens because you forgot to include one of the header files. I was also getting the same kind of error, but after proper inclusion of the header file the error went away.

发生这种情况是因为您忘记包含头文件之一。我也遇到了同样的错误,但在正确包含头文件后,错误消失了。

回答by Smotyn

Just has to include a class which seemed to be overlooked:

只需要包含一个似乎被忽视的类:

#import "NameOfClass.h"

and it was sorted. Thanks.

它被排序。谢谢。

回答by PhilipS

You can also check for dumb spelling mistakes. For example, I had a class called 'MyUIViewController' but I wrote:

您还可以检查愚蠢的拼写错误。例如,我有一个名为“MyUIViewController”的类,但我写道:

@property (nonatomic, retain) MyViewController *myViewController; 

And got this error. After I corrected it to:

并得到这个错误。在我更正后:

@property (nonatomic, retain) MyUIViewController *myViewController; 

The error was fixed, computers are so literal.

错误已修复,计算机是如此文字。

回答by Matthias D

I just had this issue in an Objective-C++ project - in that case, it's one of the errors that can crop up if you forget to name your implementation file .mm instead of .m.

我刚刚在一个 Objective-C++ 项目中遇到了这个问题 - 在这种情况下,如果您忘记将实现文件命名为 .mm 而不是 .m,这就是可能出现的错误之一。

回答by JJ Rohrer

I ran into this error when trying to reference a protocol I had set up myself.

我在尝试引用我自己设置的协议时遇到了这个错误。

This shows my protocol, the wrong method, and what I think is right method.

这显示了我的协议,错误的方法,以及我认为正确的方法。

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


Just for completeness, here is how I'm implementing some methods that also rely on the protocol...

为了完整起见,这里是我如何实现一些也依赖于协议的方法......

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;
}

Hope this helps someone else.

希望这对其他人有帮助。

回答by Binarian

If you deleted the Precompiled Prefix Header you must manually import the header-files needed into the the file:

如果您删除了 Precompiled Prefix Header,则必须手动将所需的头文件导入到该文件中:

like

喜欢

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