I've searched but I did not get with the solution. I have a class which inherits from NSObject. It has the following imports:
#import <Foundation/Foundation.h>
#import "Constants.h"
#import "CommonProtocols.h"
#import "SomeClass.h"
#interface SomeComtroller : NSObject
Than I have SomeClass.
#import "SomeController.h"
#interface SomeClass : NSObject
{
SomeController *myController; // ERROR!
}
#end
However, I can't define SomeController in SomeClass. It gives me the error, I really want to have a property of SomeController in SomeClass, and have in the controller a property of someClass.
You have a circular dependency — SomeClass.h imports SomeController.h, which imports SomeClass.h, which imports…
The solution is to move the imports into the implementation files, and just forward-declare the other classes you're using with the #class directive (e.g. #class SomeController; in SomeClass.h).
Related
Why I get this error "Unknown type name 'XMLParser'; did you mean 'NSXMLParser'?" ? I have imported XMLParser.h, so I can't understand what the problem is.
#import <UIKit/UIKit.h>
#import "XMLParser.h"
#import "televisionList.h"
#import "ListingCell.h"
#interface TelevisionDetail : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
XMLParser *xmlParser;
}
#end
Seems like Xcode bug, but also it's a good practice to avoid unnecessary import in .h file.
So try to add this before #interface..:
#class XMLParser;
And add #import "XMLParser.h" in your .m file.
I have two classes where both of them have protocols to be implemented.
Can I implement one of the class's protocol in to the other and vice versa?
Does this cause any run time error?
Your problem is cyclic dependencies. Forward declaring won't really help either as you'll just get the compiler warning you that it can't see the definitions of the protocols. There are two options:
Option 1
Split the protocols out into their own header files:
ClassA.h:
#import <Foundation/Foundation.h>
#import "ClassBProtocol.h"
#interface ClassA : NSObject <ClassBProtocol>
#end
ClassB.h:
#import <Foundation/Foundation.h>
#import "ClassAProtocol.h"
#interface ClassB : NSObject <ClassAProtocol>
#end
ClassAProtocol.h:
#import <Foundation/Foundation.h>
#protocol ClassAProtocol <NSObject>
...
#end
ClassBProtocol.h:
#import <Foundation/Foundation.h>
#protocol ClassBProtocol <NSObject>
...
#end
Option 2
If you don't care about declaring externally that you implement the protocols, then you could use the class continuation category:
ClassA.h:
#import <Foundation/Foundation.h>
#interface ClassA : NSObject
#end
ClassA.m:
#import "ClassA.h"
#import "ClassB.h"
#implementation ClassA () <ClassBProtocol>
#end
#implementation ClassA
#end
ClassB.h:
#import <Foundation/Foundation.h>
#interface ClassB : NSObject
#end
ClassB.m:
#import "ClassB.h"
#import "ClassA.h"
#implementation ClassB () <ClassAProtocol>
#end
#implementation ClassB
#end
But, I dont suppose this is really necessary. Once you have a delegate implemented then you could send a message to the other class as it the same delegate object. I suppose a simple delegate relations as;
Class A
#protocol ClassAProtocol;
#interface ClassA:NSObject
#property(nonatomic, assign) id<ClassAProtocol> delegate;
#end
#protocol ClassAProtocol <NSObject>
-(void)classA:(ClassA*)classa didSomething:(id)object;
#end
#implementation ClassA
-(void)classAFinishedSomething{
[self.delegate classA:self didSomething:nil];
[(ClassB*)self.delegate doSomething];
}
#end
Class B
#interface ClassB : NSObject<ClassAProtocol>
-(void)doSomething;
#end
#implementation ClassB
-(void)doSomething{
}
-(void)classA:(ClassA*)classa didSomething:(id)object{
}
#end
This will not create a circular reference between the two objects and keep the code clean I suppose. This logic is valid if the delegate is always class B and it respondsToSelector:, the one being sent as a message to class B. But, the above sample posted by matt is mode rigid if you want to have isolate the log between the classes and communicate via standard mechanism.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
cannot find interface declaration for ‘AbstractPickerView’,superclass of ‘AttackLayer’
There are 3 header files (a superclass and 2 subclasses):
// A.h
#interface A : NSObject
#end
// A1.h
#import "A.h"
#interface A1 : A
#end
// A2.h
#import "A.h"
#interface A2 : A
#end
I want to import all subclasses of class A.
So I added 2 #import in A.h like this:
// A.h
#import "A1.h" // added
#import "A2.h" // added
#interface A : NSObject
#end
But after adding them, the following compile errors occurred.
Cannot find interface declaration for 'A', superclas of 'A1'
Cannot find interface declaration for 'A', superclas of 'A2'
I know that this error can be solved by createing and importing the following file.
// ASubclasses.h
#import "A1.h"
#import "A2.h"
But I think #import A.h is more simple than #import ASubclasses.h to import subclasses.
And if I can use #import A.h to import subclasses, I don't need to create another file(ASubclasses.h).
Is there a way to use #import A.h to import all subclasses of A?
Or in general, how do you import all subclasses of a class?
The error you're getting is because of an #import cycle . This happens when two header files import each other . You can just declare the class using #class and then import the headerfile in the implementation file.
I have two objects, both of which are view controllers. The first (Ill call it viewController1) declares a protocol. The second (which unsurprisingly I will name viewController2) conforms to this protocol.
XCode is giving me a build error of: 'Cannot find protocol declaration for viewController1'
I have seen various questions on this subject and I am certain it is to do with a loop error, but I just can't see it in my case...
Code below..
viewController1.h
#protocol viewController1Delegate;
#import "viewController2.h"
#interface viewController1 {
}
#end
#protocol viewController1Delegate <NSObject>
// Some methods
#end
viewController2.h
#import "viewController1.h"
#interface viewController2 <viewController1Delegate> {
}
#end
Initially, I had the import line in viewController1 above that of the protocol declaration. This was preventing the project from building at all. After searching on SO, I realised the problem and switched the two lines around. I am now getting a warning (as opposed to an error). The project builds fine and actually runs perfectly. But I still feel there must be something wrong to be given a warning.
Now, as far as I can see, when the compiler gets to viewController1.h, the first thing it sees is the declaration of the protocol. It then imports the viewController.h file and sees this implements this protocol.
If it were compiling them the other way around, it would look at viewController2.h first, and the first thing it would do is import viewController1.h the first line of which is the protocol declaration.
Am I missing something?
Remove this line from viewController1.h:
#import "viewController2.h"
The problem is that viewController2's interface is preprocessed before the protocol declaration.
The general structure of the file should be like this:
#protocol viewController1Delegate;
#class viewController2;
#interface viewController1
#end
#protocol viewController1Delegate <NSObject>
#end
A.h:
#import "B.h" // A
#class A;
#protocol Delegate_A
(method....)
#end
#interface ViewController : A
#property(nonatomic,strong)id<ViewControllerDelegate> preViewController_B;(protocol A)
#end
B.h:
#import "A.h" // A
#class B;
#protocol Delegate_B
(method....)
#end
#interface ViewController : B
#property(nonatomic,strong)id<ViewControllerDelegate> preViewController_A;(protocol B)
#end
A.m:
#interface A ()<preViewController_B>
#end
#implementation A
(implement protocol....)
end
B.m:
#interface B ()<preViewController_A>
#end
#implementation B
(implement protocol....)
#end
For those who might need it:
It's also possible to fix this by moving the importation of ViewController1.h in ViewController2's implementation file (.m) instead of the header file (.h).
Like so:
ViewController1.h
#import ViewController2.h
#interface ViewController1 : UIViewController <ViewController2Delegate>
#end
ViewController2.h
#protocol ViewController2Delegate;
#interface ViewController2
#end
ViewController2.m
#import ViewController2.h
#import ViewController1.h
#implementation ViewController2
#end
This will fix the case where the error happens because ViewController1.h is imported in ViewController2.h before the protocol declaration.
I have ForceGaugeViewController class and ForceGaugeController class. I'm trying to make the ForceGaugeController class be a subclass of ForceGaugeViewController, but I'm receiving errors.
Error:
Cannot find interface declaration for ForceGaugeViewController
superclass of ForceGaugeController class.
ForceGaugeViewController.h
#import <UIKit/UIKit.h>
#import <math.h>
#import "HardwareController.h"
#import "ForceGaugeController.h"
#interface ForceGaugeViewController : UIViewController{
}
end
ForceGaugeViewController.m
#import "ForceGaugeViewController.h"
#implementation ForceGaugeViewController
ForceGaugeController.h
#import <Foundation/Foundation.h>
#import "ForceGaugeViewController.h"
#import "FMDatabase.h"
#import "FMResultSet.h"
#class ForceGaugeViewController;
// error here
#interface ForceGaugeController : ForceGaugeViewController{
}
#end
ForceGaugeController.m
#import "ForceGaugeController.h"
You can not only forward reference a class that you will inherit from or a protocol that you will implement. You just need to import the superclass in the header which you are already doing and then remove the #class declaration.
Edit: Also the superclass ForceGaugeViewController should not include the subclass ForceGaugeViewController in the header file.
ForceGaugeController.h
#import <Foundation/Foundation.h>
#import "ForceGaugeViewController.h"
#import "FMDatabase.h"
#import "FMResultSet.h"
#interface ForceGaugeController : ForceGaugeViewController{
}
#end
Be sure to avoid circular imports:
Make sure you do not import any of the header or implementation subclasses files into the superclass file where the superclass interface declaration is (.h file)
You're including ForceGaugeController.h in ForceGaugeViewController.h
and including ForceGaugeViewController.h in ForceGaugeController.h. Circular includes will confuse the compiler and that's probably what's wrong.
A header file for a class only needs to include the framework (ie. UIKit), the subclass, and any protocols the class conforms to. Forward declarations will do for the classes of instance instance variables/method arguments/properties.
Remove the line:
#class ForceGaugeViewController;
You are already importing the ForceGaugeViewController class via its header file at the top of your ForceGaugeController.m file.
EDIT 1
And as pointed out by #Chris Devereux, you have a circular reference in your header files, you will want to get rid of that as well.
EDIT 2
And not sure how I missed the self-import in your ForceGaugeController.h file. I assume that it's a typo, but if not I'm sure you know you have to remove it.