Question

I am making a photo upload to Facebook app and I think I need two @interfaces in my .h file for my View Controller.

Here is my ViewController.h file.

#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

When I try to build this code on to my iPhone or a Emulator it says

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

Thanks in advance for the help.

-Condrum.

Was it helpful?

Solution

The pattern is to put a single public interface into the .h file:

@interface FirstViewController : UIViewController

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

@end

Then put the second @implementation in the .m file as a 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

Note, this second interface uses the () syntax, which indicates that the interface is extending a previously defined interface.

But there's no point in putting both of those interfaces in the same .h file (why have two interfaces; it would be more logical to combine them into one). The primary value of the private class extension is you can extend your interface with details that only the implementation cares about, and avoid cluttering your nice simple public interface. So generally, keep public interface in the .h file, and move the private stuff into the class extension in the .m file.

For more information, see Class Extensions Extend the Internal Implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top