Sorry, couldn't find a more appropriate title.
In My code I have two classes which should know of each others existence. So I use an instance variable which points to the other class. For that to work (I guess?) the other classes headers file should be imported so it knows which methods it has and such.
Here is my code (stripped down)
MainMenuController.h:
#import <Cocoa/Cocoa.h>
#import "IRCConnection.h"
#interface MainMenuController : NSViewController {
IRCConnection *ircConnection;
}
#property (strong) IRCConnection *ircConnection;
#end
IRCConnection.h:
#import <Foundation/Foundation.h>
#import "MainMenuController.h"
#interface IRCConnection : NSObject {
MainMenuController *mainMenuController;
}
#property (strong) MainMenuController *mainMenuController;
#end
As you can see they both import each other, but this creates an error (Unknown type name 'IRCConnection') in one, and in the other Unknown type name 'MainMenuController'.
However when the connection is just one way (e.g. only MainMenuController knows about IRCConnection) and thus there is only an import statement in one of the two, it works fine.
How can I have them to know about each other? In both ways.
Hope this question makes any sense.
you could remove the import from IRCConnection.h and use a #class statement instead.
like this:
#import <Foundation/Foundation.h>
#class MainMenuController;
#interface IRCConnection : NSObject {
then add a #import "MainMenuController.h" to IRCConnection.m
In the header, use forward declaration:
#class IRCConnection;
#interface MainMenuController : NSViewController {
IRCConnection *ircConnection; // ok
}
In the source file (.m), do #import.
You cannot have circular imports. You need to break them up, or introduce some forward declarations.
Related
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.
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.
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
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I have references between two classes in Objective-C?
Objective-C #import loop
I'm getting a couple errors in my code and I'm not sure but I think its because I'm #importing an interface inside another interface where I'm #importing the other interface. If I'm confusing you I'll give you an example.
#import "OneClass.h"
#interface SecondClass : NSObject
{
OneClass * obj;
}
#import "SecondClass.h"
#interface OneClass : NSObject
{
SecondClass * obj;
}
Yes, you have a circular import. The problem here is that the second import (the one that re-imports your first header) is basically ignored by the compiler, since it thinks it's already imported that header.
The solution here is to use #class forward-declarations instead of using #imports. Not only does this solver the circular import problem, but it's a better idea anyway since it breaks unnecessary dependency chains (e.g. if I edit OneClass.h, SecondClass.h won't need to be re-processed).
To apply this here, simply remove the #import OneClass.h in SecondClass.h and replace it with #class OneClass;
In the more general case, you don't ever need to #import a header file just to declare an ivar/property/method that uses a class from that header. The #class token is sufficient. You do, however, need to #import the header file if you're inheriting from the class, or if you're referencing another non-class type declared in that header. Also remember that if you use #class in your header, you need to remember to put the actual #import into your .m file.
If you're importing a header file you need to put the full file name. In this case...
#import "SecondClass.h" instead of #import "SecondClass"
You can declare the use of a class without having to #import its associated header, like so:
// #import "SecondClass.h" // no need for this anymore
#class SecondClass;
#interface OneClass : NSObject
{
SecondClass * obj; // OK
}
When there are no physical dependencies, you should be using forward declarations to minimise your build times:
// SecondClass.h
#class OneClass;
#interface SecondClass : NSObject
{
OneClass * obj;
}
#end
// OneClass.h
#class SecondClass;
#interface OneClass : NSObject
{
SecondClass * obj;
}
#end
It also happens to fix your dependency cycle ;)
I need to develop an application which has a interface which implements methods of 3 protocols.
Assume protocol A extends protocol B and protocol C, and interface implements protocol A.
This is how my code looks,
// This is in MyClass.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "protocol_A"
#interface MyClass : NSObject <protocol_A>
{
}
#end
//This is MyClass.m file
#import "MyClass.h"
#implementation myClass
-(void)methodinA
{
NSLog(#"I'm in protocol_A");
}
}
-(void)methodinB
{
NSLog(#"I'm in protocol_B");
}
-(void)methodinC
{
NSLog(#"I'm in protocol_C");
}
#end
//This is protocol_A.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "protocol_B.h"
#import "protocol_C.h"
#protocol protocol_A <protocol_B, protocol_C>
-(void)methodinA;
#end
//This is in protocol_B.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#protocol protocol_B
-(void)methodinB;
#end
//This is in protocol_C.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#protocol protocol_C
-(void)methodinC;
#end
i'm getting an exception , and my app is getting crashed...
***Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MyClass 0X323nm31>setvalue:forundefinedKey:]:this class is not key value coding-compilant for the key window'.
Plz Tel me how to solve this problem??
So where you're getting this from (and the reason you're getting it 3 times) is you've got a mistake in your protocol definitions. You have:
//This is in protocol_C.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#protocol protocol_C
{
}
-(void)methodinC;
#end
You can't declare class members in a protocol: only methods. Because of this, you don't need (and, as you've discovered) can't have the curly braces in the protocol definition. As such, you need this for your protocol definitions:
//This is in protocol_C.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#protocol protocol_C
-(void)methodinC;
#end
Removing those should solve your issue.
When making new files, I always go through Xcode's new-class-files process, as it frequently gives you lots of convenient stuff. Here is the contents of a new protocol_D declaration fresh from Xcode:
#import <Cocoa/Cocoa.h>
#protocol protocol_D
#end
Hope this helps!
TL;DR: Protocol definitions can't have curly-braces anywhere in them.
Protocols generally go in a .h file; always go in a .h file if you plan on using them anywhere.
Just like everything else, you need to #import the .h file that contains the definition of the protocol before you use it.
So, in MyClass.h (it really should be capitalized -- Classes are always capitalized in Objective-C), #import the various protocol .h files.
Your protocol_A.h file declares conformance to protocol_B and protocol_C, yet you haven't imported the headers for protocol_B and protocol_C. This means that you are declaring conformance to protocols that as far as the compiler is concerned, don't exist in protocol_A.h. You need to import the headers:
In protocol_A.h:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "protocol_B.h" //note these new imports
#import "protocol_C.h"
#protocol protocol_A <protocol_B, protocol_C>
-(void)methodinA;
#end
Also see Apple's Communicating with Objects, which discusses delegates, protocols, and selectors. Though its listed under Mac OS X, most (if not all) appears to apply to iOS also.