First, I have seen this question as well as this, but my problem is not addressed there.
I have a protocol ProtocolA defined in its own header file. Then I have two classes ClassA and ClassB which both conform to this protocol so the protocol-header is imported in their header files.
Now it gets a bit complicated. ClassA is used (and thus imported) in a third ClassC. This class conforms to a second protocol ProtocolB. This protocol also has its own header file where it uses and imports ClassB. So my ClassC imports (either directly or indirectly) both ClassA and ClassB (which both import ProtocolA). This gives me the following warning regarding ProtocolA:
warning: duplicate protocol definition of '…' is ignored
Why is this happening? It was my understanding that the #import macro was invented exactly for avoiding this kind of problems which we had with #include. How can I solve the issue without using an include guard? I can't really remove any of the imports.
EDIT: here is the code to illustrate the situation:
ProtocolA.h
#protocol ProtocolA <NSObject>
- (void)someMethod;
#end
ClassA.h
#import "ProtocolA.h"
#interface ClassA : NSObject <ProtocolA>
...
#end
ClassB.h
#import "ProtocolA.h"
#interface ClassB : NSObject <ProtocolA>
typedef enum Type {
TypeB1,
TypeB2
} TypeB;
...
#end
ProtocolB.h
#import "ClassB.h"
#protocol ProtocolB <NSObject>
- (TypeB)someMethod;
#end
ClassC.h
#import "ProtocolB.h"
#interface ClassC : NSObject <ProtocolB>
...
#end
ClassC.m
#import "ClassC.h"
#import "ClassA.h" // the warning appears here
#implementation ClassC
...
#end
do not Import ClassB in ProtocolB header, just use #class ClassB; in it and remove #import "ClassB"
Related
I've read that you should try to use #class in your header file instead of #import but this doesn't work when your #class contains a delegate protocol that you're trying to use.
MyView.h
#import <UIKit/UIKit.h>
#class MyCustomClass; // <-- doesn't work for MyCustomClassDelegate, used below
#interface MyView : UIView <MyCustomClassDelegate>
#end
I think I'm overlooking something, is there a way to get #class to work in this situation or is #import my only choice?
Edit: One work around for this is, of course, declaring your #import MyCustomClass and MyCustomClassDelegate in the private interface section of the .m file instead of the .h file.
you can use #protocol to forward declare a protocol if you only need it for variables such as this:
#protocol MyProtocol;
#interface MyClass {
id<MyProtocol> var;
}
#end
In your case the declared class is trying to conform to a protocol so the compiler must know about the protocol methods at this point in order to deduce weather or not the class conforms.
In this case, I think your options are to split the protocol into its own file and #import that header, or declare the protocol in that header above the class declaration that uses it.
You can only forward-declare a protocol in the same header file for usage in method return values or parameter types. In your case you want the class to conform to the protocol, so it won't work since it defines behavior that will be added to the class itself (i.e. the methods it will respond to).
Therefore, you must #import the protocol. For this reason, it is probably a good idea to split the protocol and class up into separate files. See this answer for more information.
MyCustomClassDelegate is a protocol, not a class. Telling the compiler about the existence of MyCustomClass tells it nothing about the existence of the protocol.
You will need to declare your delegate protocol before the class:
MyCustomClass.h:
#import <UIKit/UIKit.h>
#class MyCustomClass;
#protocol MyCustomClassDelegate <NSObject>
- (void)myCustomClass:(MyCustomClass *)customClass
didBlah:(BOOL)blah;
#end
#interface MyCustomClass : NSObject <MyCustomClassDelegate>
#end
And you cannot even use #protocol to forward-declare the delegate protocol; the compiler must see the complete declaration, therefore change your #class for an #import:
MyView.h:
#import <UIKit/UIKit.h>
#import "MyCustomClass.h" // the compile now knows what MyCustomClassDelegate is
#interface MyView : UIView <MyCustomClassDelegate>
#end
You cannot forward declare a protocol you conform to.
If you are using MyView as a MyCustomClassDelegate only in MyView's implementation, you can use Extension in MyView's .m file, such as this:
#import "MyView.h"
#import "MyCustomClassDelegate.h"
#interface MyView () <MyCustomClassDelegate> {
}
#end
#implementation MyView
...
#end
Basically, I want to call function from one class to another.I know I need to do delegate.
However, for example, there is class A and B. I need to call function in class A from class B and I also need to call function in class B from class A.
As a result, I need to import like
import "classA.h" //to do in class B
import "classB.h" //to do in class A
//then i may have delegate like this in both class
#protocol emailingroupViewControllerDelegate <NSObject>
-(void)updateGroupEmail :(NSString *)inputKey;
#end
The problem is that it didn't allow me to import like this in between two class. How should I do?
You have a dependency cycle in your headers. It's easy to work around; instead of including the other class' header file in each header, you use the #class directive and include the header file in the implementation file.
In classA.h, do this:
#class ClassB
#interface ClassA
...
#end
In classA.m, do this:
#import "classA.h"
#import "classB.h"
#implementation ClassA : NSObject
...
#end
In classB.h, do this:
#class ClassA
#interface ClassB
...
#end
Finally, in classB.m, do this:
#import "classB.h"
#import "classA.h"
#implementation ClassB : NSObject
...
#end
However, if you're trying to use the delegate pattern, your delegate should implement a protocol rather than be an explicit class. You should set up the connection between the delegate and the object that uses it in a third class. This would be the header for ClassA:
#protocol SomeDelegate <NSObject>
- (void)classA:(ClassA *)classA didSomethingWithAString:(NSString *)string;
#end
#interface ClassA : NSObject
#property (weak, nonatomic) id <SomeDelegate> delegate;
#end
However, again, if what you're trying to do is just have ClassA call a function in ClassB, which can call a function in ClassA, you might want to re-examine the way you've designed your classes.
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
I have two classes ClassA and Class B (they are viewControllers).
Class A is a delegate of classB.
ClassA "laucnhes" and instance of ClassB.
ClassB call methods on classA.
Let's say it's :
#import "ClassB.h"
#interface ClassA : NSObject {
ClassB* subController;
}
- (void) doThis;
-------------------------------
#import "ClassA.h"
#interface ClassB : NSObject {
ClassA* delegate;
}
-------------------------------
#implementation ClassB
- (void) someMethod {
AnObject* myObj = [self.delegate.arr objectAtIndex:8];
[self.delegate doThis];
}
Doing that way, A must import B, and B must import A.
If B do not import A (with just #class A), there is a compile error for the used attribute from A.
If B imports A, there is a compile error on the ClassA* delegateline.
Why do I have those compile errors? Doesn't #import protect again recursive calls ?
I don't need a solution to solve that problem, I know how I may do this.
But I wonder why my #import cause those problems. These are not #includes...
In .h files prefer #class to #import. Both can then be imported in the .m implementation files.
// ClassA.h -------------------------------
#class ClassB;
#interface ClassA : NSObject {
ClassB* subController;
}
- (void) doThis;
// ClassB.h -------------------------------
#class ClassA;
#interface ClassB : NSObject {
ClassA* delegate;
}
// ClassB.m -------------------------------
#import "ClassA.h"
#import "ClassB.h"
#implementation ClassB
- (void) someMethod {
AnObject* myObj = [self.delegate.arr objectAtIndex:8];
[self.delegate doThis];
}
Using #class statements instead of #import also reduces dependencies and makes the remaining ones more clear. It can also speed up compile times.
Why do I have those compile errors? Doesn't #import protect again recursive calls ?
#import protects against repeatedly importing the same header into the same module, whether by circular includes/imports or not. It protects against that by not letting you do it: Only the first #import of a header works; subsequent #imports of the same header are ignored.
In a circular #include situation, the preprocessor would go around the circle some number of times and then fail the build before you even get to compilation. Using #import prevents the preprocessor from getting wedged and lets the preprocessor succeed, but circular-#import code is still dodgy at best and usually will not compile.
So, on to your specific situation.
For the code you showed in your question, #class will work in either or both headers, and indeed you should use it in both. You'll also need to #import both headers in both .m files.
If B do not import A (with just #class A), there is a compile error for the used attribute from A.
If you mean “there is a compile error at each point where I use that attribute of type ClassA *”, then yes: You can't talk to that object because you haven't imported its interface, so the compiler doesn't know what messages you can send to a ClassA instance. That's why you need to import its interface.
If B imports A, there is a compile error on the ClassA* delegateline.
If both headers import each other, then you have this:
ClassA.m:
ClassA.h
ClassB.h
ClassA.h (ignored because this was already imported by ClassA.m)
ClassB.m:
ClassB.h
ClassA.h
ClassB.h (ignored because this was already imported by ClassB.m)
There is no way this can work without one interface preceding the other without the other interface preceding it. That's the circle you're running into—the circle that #import exists to break. #include allows the circle, and thereby gets wedged:
ClassA.m:
ClassA.h
ClassB.h
ClassA.h
ClassB.h
ClassA.h
ClassB.h
ClassA.h
ClassB.h
ClassA.h
ClassB.h
ClassA.h
ClassB.h
(fails at some point)
Hence #import.
So you cannot import each header from the other. Hence #class.
But you still need to import each header from each module. That is, in fact, exactly what you need to do: Use #class in each header and use #import (on both headers) in each module.
This compile complaint can be avoided by declaring
#class ClassB;
in the .h file. The ClassB.h can then be included into the .m file.
So you are right on that one. Contrary to urban myth, #imports work pretty much like #includes in the sense that the compiler has to check the file.
See this (duplicate?) question for your philosophical problem.
I think you'll find that #import only protects against multiple inclusion once it has already been successfully included once, so to speak.
ie, in your case, it hasn't successfully imported classa.h before it is asked to import it again, so it does so.
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.