The keyword of class and interface at header file in Objective-c [duplicate] - objective-c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
#class vs. #import
I am new to Objective-c, I have seen an example look likes:
#import <UIKit/UIKit.h>
#class MapKitSampleViewController;
#interface MapKitSampleAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MapKitSampleViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet MapKitSampleViewController *viewController;
#end
The above code is stored at "MapKitSampleAppDelegate.h" file, I want to ask what is the meaning of line 3 "#class MapKitSampleViewController;"? Can we change it to #import "MapKitSampleViewController.h"?

I want to ask what is the meaning of line 3
"MapKitSampleViewController"? Can we change it to #import
"MapKitSampleViewController.h"?
Yes.
#class keyword is a "forward declaration". What you are telling the compiler is that this class is going to be used in this class, but the header import for it will be elsewhere.
Most likely if you look in the .m file, you will find that the #import "MapKitSampleViewController.h" will be there.
Why?
The reason why this was implemented (I think, anyway), is to prevent circular imports. Imagine a scenario where the following happens:
Class1.h
#import Class2.h
Class2.h
#import Class1.h
Now, if I'm not wrong, what happens here is that during compilation, it will repeatedly import both header and bad things happen. The #class keyword is meant to prevent this from happening, because the import for those files will happen in the .m files, not in the .h files.
BTW this is a duplicate of #class vs. #import
So you will likely find more in-depth discourse on this topic at that question.

Yes you can change it but this will increase the compilation time and will bring you no benefits.
The "#class MapKitSampleViewController;" is a forward declaration see http://en.wikipedia.org/wiki/Forward_declaration
When using forward declaration you have to take care that you can use the forward declared class name only for type references.

Related

Cannot find interface declaration for superclass of

I know this is a common subject but I did not find any answer to help me out with this problem.
Well, I do have (by now) 3 .h files,
One is the superclass of the two others.
Superclass : ExportationAutoFilterNew
#import <Foundation/Foundation.h>
#import <HIDDEN/PluginFilter.h>
#class MyOutlineView;
#interface ExportationAutoFilterNew : PluginFilter
#property (assign, nonatomic) NSUserDefaults *prefs;
#property (assign, nonatomic) MyOutlineView *databaseOutline;
#end
Then, sub-classes : DetectNewExams & ExportExams
#import <Foundation/Foundation.h>
#import <HIDDEN/PluginFilter.h>
#import "ExportationAutoFilterNew.h"
#interface DetectNewExams : ExportationAutoFilterNew
- (void)detectExams:(NSDate*)currentDate timeInterval:(double)timeInterval afterExitHidden:(BOOL)check;
And
#import <Foundation/Foundation.h>
#import <HIDDEN/PluginFilter.h>
#import "ExportationAutoFilterNew.h"
#interface ExportExams : ExportationAutoFilterNew
- (void)exportExams:(NSManagedObject*)curObj;
The .m goes like this
Superclass : ExportationAutoFilter
#import <HIDDEN/PreferencesWindowController.h>
#import <HIDDEN/browserController.h>
#import "ExportationAutoFilterNew.h"
#import "DetectNewExams.h"
#implementation ExportationAutoFilterNew
Xcode for this file (ExportationAutoFilterNew.m) :
Instance method '-detectExams:timeInterval:afterExitHidden:' not found (return type defaults to 'id')
For the call of :
[self detectExams:lastExportDate timeInterval:0 afterExitHidden:YES];
Sub-classes : DetectNewExams
#import <HIDDEN/PreferencesWindowController.h>
#import <HIDDEN/browserController.h>
#import "DetectNewExams.h"
#import "ExportExams.h"
#implementation DetectNewExams
In this file (DetectNewExams.m), Xcode says :
Instance method '-exportExams:' not found(return type defaults to 'id')
Calling :
[self exportExams:curObj];
And then : ExportExams
#import <HIDDEN/PreferencesWindowController.h>
#import <HIDDEN/browserController.h>
#import "ExportExams.h"
#implementation ExportExams
I'm wasting hours on this thing and I can't figure the problem out, may you help me ? I looked up for compiling circle but I don't think I created one with my imports ..
Thanks.
Your interface is not properly declared. Try this.
#interface ExportationAutoFilterNew : PluginFilter
#property (nonatomic) NSUserDefaults *prefs;
#property (nonatomic) MyOutlineView *databaseOutline;
#end
Now that you have posted the actual error, this becomes more clear. But you still haven't posted enough context.
Instance method '-detectExams:timeInterval:afterExitHidden:' not found (return type defaults to 'id')
That means that the method declaration hasn't been seen at the time the code was compiled or the method is declared on some class that whatever was being called is not an instance of.
Since you haven't shown the call site or the declaration of the variable being called, this is a guess. The method is declared on the DetectNewExams class. I'd bet you have a variable that points to an instance of ExportationAutoFilterNew and, therefore, the compiler is correctly warning that the method doesn't exist on the class.

Unknown type name 'AClass' in objective c

In my MyClass.h, I have this:
#import "AClass.h"
#interface MyInterface : UIViewController <UIScrollViewDelegate>
#property (strong, nonatomic) AClass *ptr;
#end
But when I compile it, I get an error saying Unknown type name 'AClass' , did you mean 'BClass'.
What I don't understand it I already have "#import "AClass.h", how can I fix my error?
In AClass.h, I have
#interface AClass : UICollectionViewFlowLayout
#end
And both AClass.h and MyClass.h are in the same directory.
Thank you.
(This answer is obsolete since the question was changed including the displayed code)
It's your headerfile, right? So don't import yourself.
In MyClass.m you need #import "MyClass.h", but not in the header file of MyClass itself.

Need little help incomplete implementation error in Objective-C

I'm getting an "incomplete implementation" error in my .m file, but I cannot figure it out how to fix it. I`ll post .h and .m files if you can give me tips on how to fix that. Thanks.
Apparently, I need to put more details, or I cannot post the question because the post contains mostly code, so this are just some dummy lines.
.h File
#import <UIKit/UIKit.h>
#interface BlogViewController : UIViewController <UIPickerViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
- (IBAction)selectPicturePressed:(id)sender;
- (IBAction)blogPost:(id)sender;
#property (weak, nonatomic) IBOutlet UITextView *commentTextField;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#property (nonatomic, strong) NSString *username;
#end
.m File
#import "BlogViewController.h"
#import <Parse/Parse.h>
#import "SWRevealViewController.h"
#import "PhotoViewController.h"
#interface BlogViewController ()
-(void)showErrorView:(NSString *)errorMsg;
#end
#implementation BlogViewController **//Incomplete Implementation**
#synthesize imageView = _imageView;
#synthesize username = _username;
#synthesize commentTextField = _commentTextField;
IBActions are just regular functions with syntactic sugar for connecting them to interface builder therefore you have to implement them in the .m file
.m file:
- (IBAction)selectPicturePressed:(id)sender {
// code here
}
- (IBAction)blogPost:(id)sender {
// and here
}
On the line that's giving you the Incomplete Implementation error, you can get more details about what you're missing.
You don't paste all your .m so it's anyone's guess what you're missing, however, your .h declares 2 methods and 3 protocols that you must implement.
Your .m file must have method bodies for these two methods:
- (IBAction)selectPicturePressed:(id)sender;
- (IBAction)blogPost:(id)sender;
Likely, you already have these in here, particularly if these were generated by Ctrl+Dragging from interface builder.
But you must also include, at a minimum, all the required methods from the protocols you declared.
UIPickerViewDelegate protocol official documentation
UINavigationControllerDelegate protocol official documentation
UIImagePickerControllerDelegate protocol official documentation.
(I'm not completely familiar with these protocols and don't know for sure that they actually have any #required methods.)
Your .m also has a private interface, which declares a method you must implement in the implementation.
-(void)showErrorView:(NSString *)errorMsg;
You declared this method in a private interface, so be sure to implement this method as well.
Regardless of what you're missing, Xcode will absolutely tell you if you just click on the error/warning. Xcode will give you the names of the methods its expecting to find in your implementation but cannot.

Unknown Type Name in Xcode (even with #class declaration)

I'm having trouble with a simple app setting up a data controller. I get an error on the line #property (strong, nonatomic) BirdsListDataController *dataController; in BirdsListViewController.h. I've tried my best to use a #class declaration of BirdsListDataController, as well as trying to remove any #import statements from the .h files and tried to remove a circular #import which you can find commented out in the top of BirdsListViewController.h. I'm guessing it's something simple.
BirdsListViewController.h
#import <UIKit/UIKit.h>
#class BirdsListDataController;
#interface BirdsListViewController : UITableViewController <UITextFieldDelegate>
{
// NSMutableArray *listOfBirds;
IBOutlet UITextField *addNewBirdTextField;
}
//#property (nonatomic, retain) NSIndexPath *checkedIndexPath;
#property (nonatomic, retain) NSString *textLabelContents;
#property (nonatomic, retain) NSMutableArray *workingArray;
#property (strong, nonatomic) BirdsListDataController *dataController;
#property (strong, nonatomic) IBOutlet UITableView *birdListTableView;
#end
BirdsListViewController.m
#import "BirdsListViewController.h"
#import "BirdsListDataController.h"
#interface BirdsListViewController ()
#end
#implementation BirdsListViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
...
BirdsListDataController.h
#import <Foundation/Foundation.h>
#class BirdName;
#interface BirdsListDataController : NSObject
#property (nonatomic, copy) NSMutableArray *listOfBirds;
-(NSUInteger)countOfList;
-(BirdName *)objectInListAtIndex:(NSUInteger)theIndex;
-(void)addBirdNameWithName:(BirdName *)bName;
#end
BirdsListDataController.m
#import "BirdsListDataController.h"
//#import "BirdsListViewController.h"
#import "Bird.h"
#implementation BirdsListDataController
-(id)init
{...
I'm still really new to iOS and Objective C, so hopefully my code isn't too awful to troubleshoot. Thanks for the help.
For people looking for a better answer than comment/uncomment your code, a better solution is to clean your project and to delete your derived data. Once you've fixed your circular references, the keystroke Command+Shift+K will clean your project, or you can go to and select Product->Clean.
To delete your derived data, open Organizer, click on the Projects tab, navigate to your project in the sidebar. You should see "Derived Data" under the project name header. To the right of that should be a button saying delete. If it is enabled, deleting the derived data can also remove hanging errors.
As way of explanation, it seems sometimes that Xcode becomes out of sync with a project, holding on to errors that no longer exists. This is better in more recent version, but still happens occasionally.
I'm not certain what is causing your problem, but a few things:
In the code that you've presented there is no reason not to import BirdListDataController.h in BirdListViewController.h, since there is no reference to BirdListViewControllers in BirdListDataController.h. So try replacing your #class declaration with an #import statement.
In BirdListDataController.h you declare #class BirdName, but in BirdListDataController.m you import Bird.h instead of BirdName.h. It seems like something could be wrong there, although I would have to see the code for BirdName.h and Bird.h to know for sure.
In my case I had duplicate class names in different folders structure, Once I removed the new class and named it differently everything worked again.
So to translate this into a practical solution, as per "shA.t"'s comment:
if you comment/uncomment your code, or clean project as above
answers suggested but still doesnt solve it:
take a step to look back at recent classes changes and double check
all class names are unique even if in different directories, double
check
them all
if duplicate class name found: make a backup of that
code, delete that class (not just reference, but to trash too)
create a new class with unique name and incorporate the backed up
code
For this particular duplicate class name scenario, this will save you the hassle of importing and commenting your #import "class.h"

Xcode warns about missing protocol definition, even though #protocol is used

Since I had a import-cycle recently, I'm moving all #import statements (concerning my own files) from the header into the corresponding .m-file. I also added #class and #protocol forward-declarations to soothe the compiler. However, I still get he following warning:
Cannot find the protocol definition for 'MyCustomDelegate'.
As I said, there is an #protocol MyCustomDelegate before I use it in the #interface-Block. Interestingly this warning only occurs if the corresponding delegate is declared in another file (whose header is imported in the .m-file).
I read that one solution is to declare the delegate in a separate header file and import that file directly in the header of the class that implements the delegate. Is this really the way to go? Are there any other solutions? I think those delegates already bloated our code enough, now I should go on and even declare an own file for it?
Small sample code to better illustrate the problem:
NewFooController.h
#import <UIKit/UIKit.h>
#protocol NewFooControllerDelegate;
#interface NewFooController : UITableViewController
#property (nonatomic, weak) id<NewFooControllerDelegate> delegate;
#end
#protocol NewFooControllerDelegate
#end
HomeTableViewController.h
#import <UIKit/UIKit.h>
#protocol NewFooControllerDelegate;
// warning points to line below
#interface HomeTableViewController : UITableViewController <NewFooControllerDelegate>
#end
HomeTableViewController.m
#import "HomeTableViewController.h"
#import "NewFooController.h"
#implementation HomeTableViewController
#end
HomeTableViewController.h references the protocol, but it hasn't been declared yet.
If you import NewTaskController.h in HomeTableViewController.h before it attempts to use it, it should solve your problem.
Of course you can then remove the import from HomeTableViewController.m
Not sure if this is "best way", but try import header of class that implement protocol before class header file.
HomeTableViewController.m
#import "NewFooController.h"
#import "HomeTableViewController.h"
#implementation HomeTableViewController
#end
And you can remove protocol declaration in HomeTableViewController.h
#import <UIKit/UIKit.h>
#interface HomeTableViewController : UITableViewController <NewFooControllerDelegate>
#end