Iphone 错误:在目标C中的…之前应该有说明符限定符列表?
每当我构建下面的代码时,我都会得到上面的错误Iphone 错误:在目标C中的…之前应该有说明符限定符列表?,iphone,objective-c,Iphone,Objective C,每当我构建下面的代码时,我都会得到上面的错误 //Controller.h #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> #import "PolygonShape.h" #import "PolygonView.h"; @interface Controller : NSObject { IBOutlet UIButton *decreaseButton; IBOutlet UIBu
//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
#进口
#进口
#导入“PolygonShape.h”
#导入“PolygonView.h”;
@接口控制器:NSObject
{
IBUIButton*递减按钮;
IBUIButton*递增按钮;
IBUILabel*SidesLabel编号;
IB多边形形状*形状;
iBonView*shapeView;
}
-(i)减少;
-(i)增加;
-(无效)从NIB唤醒;
@结束
//控制器
#导入“Controller.h”
@执行控制器
@结束
但是,当我替换import语句并提出一个forward类引用时,代码将编译
//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
//Controller.h
#进口
#进口
#导入“PolygonShape.h”
@类PolygonView;
@接口控制器:NSObject
{
IBUIButton*递减按钮;
IBUIButton*递增按钮;
IBUILabel*SidesLabel编号;
IB多边形形状*形状;
iBonView*shapeView;
}
-(i)减少;
-(i)增加;
-(无效)从NIB唤醒;
@结束
//控制器
#导入“Controller.h”
#导入“PolygonView.h”
@执行控制器
@结束
有人能解释一下吗?是的,我也遇到了循环依赖的问题,我将两个类相互导入。我也不知道什么是前瞻性声明。我立刻在维基百科上搜索了一下,但描述得很差。我发现这篇文章解释了它们是什么以及它们与周期性进口的关系
注意:链接一直处于关闭状态,所以我只是。发生这种情况是因为您忘记包含一个头文件。我也遇到了同样的错误,但在正确地包含头文件后,错误消失了。我只犯了一个简单的错误就得到了这个错误。。。也许其他人也在这样做 正确:
@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
错:
@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
只需包含一个似乎被忽略的类:
#import "NameOfClass.h"
它被分类了。谢谢。你也可以检查哑巴拼写错误。例如,我有一个名为“MyUIViewController”的类,但我写道:
@property (nonatomic, retain) MyViewController *myViewController;
我犯了这个错误。在我将其更正为:
@property (nonatomic, retain) MyUIViewController *myViewController;
错误已经修复,计算机是如此的平淡无奇。我刚刚在Objective-C++项目中遇到了这个问题-在这种情况下,如果忘记将实现文件命名为.mm而不是.m,则可能会出现错误。请遵循以下规则以避免此类问题: 如果对象仅由内部使用 类的实现文件,使用 在头文件中转发声明,并在 实现文件。否则使用 在标题中导入
如果删除了预编译前缀头,则必须手动将所需的头文件导入文件: 像
#导入
#进口
#进口
我在尝试引用自己设置的协议时遇到此错误
这显示了我的协议,错误的方法,以及我认为正确的方法
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.h
//------------------------------------EtEmailDelegate协议-开始---------------------------------------
@协议代理
-(void)mailComposeController:(MFMailComposeViewController*)控制器未完成结果:(MFMailComposeResult)结果错误:(NSError*)错误;//{
//所有者
UIViewController*myUivc;
//EtEmailDelegate*myDelegate;//PolygonShape和PolygonView可能有周期性导入。这就是存在转发声明的原因。请使用它们。PolygonShape和PolygonView没有周期性导入,但PolygonView和Controller有。谢谢您的帮助!@Jason:这可能是一个好答案。@Chuck,不会。会的一个非常好的答案的开头。结尾是你必须解释什么是前向声明,以及为什么你要用它来避免循环声明,这对我们这些来自Java的人来说毫无意义。@Jason Coco,如果你写了这个答案,你将在未来几年中获得支持(并帮助我理解这个棘手的主题).1是头文件中循环依赖关系的一个很好的例子。这两个变量有何不同?我看不出来。属性前括号vs afteruntible..写“您应该在@end之前键入@implementation”是一样的。很明显,您应该只将类变量放在大括号中
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;
}