在同一个.h文件中有两个@interfaces

时间:2013-10-03 02:45:15

标签: objective-c facebook interface header duplicate-removal

我正在将照片上传到Facebook应用程序,我想我的.h文件中需要两个@interfaces用于我的View Controller。

这是我的ViewController.h文件。

#import <UIKit/UIKit.h>
#import <Social/Social.h>
@interface FirstViewController : UIViewController <UIImagePickerControllerDelegate,   UINavigationControllerDelegate> {
UIImagePickerController *bailey;
UIImagePickerController *baileys;
UIImage *image;
IBOutlet UIImageView *imageView;
}
-  (IBAction)TakePhoto;
-  (IBAction)ChooseExisting;
@end
@interface FirstViewController : UIViewController {   SLComposeViewController       *slComposeViewController;
UIImage *image; }
- (IBAction)ShareFB;
@end

当我尝试将此代码构建到我的iPhone或模拟器上时,它说

/Users/Condrum/Desktop/project/myApp/myApp/FirstViewController.h:21:1: Duplicate interface definition for class 'FirstViewController'

提前感谢您的帮助。

-Condrum。

1 个答案:

答案 0 :(得分:3)

模式是将单个公共接口放入.h文件中:

@interface FirstViewController : UIViewController

// in here put those public properties and method declarations that
// other classes need to have access to

@end

然后将第二个@implementation放在.m文件中作为private class extension

@interface FirstViewController () <UIImagePickerControllerDelegate,   UINavigationControllerDelegate>

// in here, place those private properties and instance variables that
// only this class needs to be aware of

@end

注意,第二个接口使用()语法,表示接口正在扩展先前定义的接口。

但是将这两个接口放在同一个.h文件中是没有意义的(为什么有两个接口;将它们组合成一个接口更合乎逻辑)。私有类扩展的主要价值是你可以使用只有实现关心的细节扩展你的界面,并避免混乱你漂亮的简单公共接口。所以通常,在.h文件中保留公共接口,并将私有内容移动到.m文件中的类扩展名中。

有关详细信息,请参阅Class Extensions Extend the Internal Implementation