Cannot find protocol declaration for - iphone

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.

Related

Forward Class and Protocols in Objective C

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.

Defining delegate function in separate file (instead of in ViewController)

I have a ViewController, and a UIView.
The UIView has a delegate, and the delegate function is set in the ViewController.
All I want to do, is have the delegate function defined in a separate file. So the UIView.m #imports the separate file, instead of all the ViewControllers which use the UIView.
I believe this is a standard procedure, but keep falling over myself trying to get it to work. :| Would really appreciate some help. Thanks.
myViewController.h
#import <UIKit/UIKit.h>
#import "myUIView.h"
#protocol ModalViewDelegate
-(void)didReceiveMessage:(NSString *)message;
#end
#interface myViewController : UIViewController <ModalViewDelegate>
#property (nonatomic, strong) myUIView *myUIViewItem;
#end
myViewController.m
#import "myViewController.h"
#import "myUIView.h"
#interface myViewController ()
#end
#implementation myViewController
#synthesize myUIViewItem;
- (void)didReceiveMessage:(NSString *)message { //<<< THIS IS WHAT
NSLog(#"Message from button: %#", message); //<<< NEEDS MOVING
}
- (void)viewDidLoad
{
…
myUIViewItem.delegate = self;
…
myUIView.h
#import <UIKit/UIKit.h>
#protocol ModalViewDelegate;
#interface myUIView : UIView {
id<ModalViewDelegate> delegate;
}
#property (nonatomic, strong) id<ModalViewDelegate> delegate;
myUIView.m
#import "myUIView.h"
#import "myViewController.h"
#implementation myUIView
#synthesize delegate;
...
[delegate didReceiveMessage:#"Data from UIView!"];
well, there is one method actually,
Just take one .h file and lets say connectionDelegate.h and declare your protocol init
In connectionDelegate.h:
#import <UIKit/UIKit.h>
#protocol ConnectionDelegate
-(void)getResult:(NSString*)_result;
#end
Then in your view controller:
#import "ConnectionDelegate.h"
#interface myViewController : UIViewController <ConnectionDelegate>
{
id delegate;
}
then in .m file, by just call the method
[delegate getResult:_result];
Edit regarding the warnings:
You need to declare the method in view controller, you need to do like this.
[self getResult:urlDataString];
-(void)getResult:(NSString*)_result{
[delegate getResult:_result];
}
Based on your comment:
I want to have the function 'didReceiveMessage' defined in a SEPARATE
file. So that I don't have to repeat it in every ViewController that
uses the UIView and delegate. e.g. ModalViewDelegate_Action.h and
ModalViewDelegate_Action.m
The way I was given was to use a subclass, and that's been working great for me. In my iOS projects I have a class called BaseViewController, which is a subclass of UIViewController. I put lots of code in it related to HUD management, NSOperations management, etc. Then virtually all my view controllers are subclasses of it.

Expected specifier-list before "MainViewController / Method -x not found return type defaults to id

I have the following imports in my EquivalenceClassGroup
#import "MainViewController.h"
but then in my property in EquivalanceClassGroup.h:
#property (nonatomic, assign) MainViewController *myController;
I get this error: "Expected specifier-list before MainViewController when compiling
If I change the import to: #class MainViewController and comment out the import of MainViewController.h that error goes away but then then XCode can't find the methods in my MainViewController from EquivalenceClassGroup.m so from here:
-(id)initWithLetterNumbers: (int)numOfLettersInWord enteredLetter: (NSString *) str controller:(UIViewController *)controller {
myController = (MainViewController *) controller;
letterArray = [myController getLetterArray];
[myController getLetterArray];
I get: "Method -getLetterArray not found return type defaults to id" on that last line
I have this method defined in the MainViewController: -(NSArray*)getLetterArray;
and there are no errors in that interface file or the m file. In the equivalence class if I type in [myController then space, I cant seem to find any methods.
From the MainViewController class I do import the EquivalenceClassGroup and use the methods without any problems
You have to make sure that you import the MainViewController.h file in your .m file. All the #class does is tell the interface that there is such a class, but it doesn't tell it anything about the class. That is what #import essentially does. The #class in the .h file is just good programming practice to make sure that you are doubling importing or anything. Hope that helps!
I think you have to write #import "MainViewController.h" in EquivalanceClassGroup.h file and
#class MainViewController in MainViewController.h file.
#class MainViewController
in your EquivalenceClassGroup.h
#import "MainViewController.h"
in EquivalenceClassGroup.m
This is called "forward declaration" and is quite common.

UIViewController calling each other's delegate

I have two UIViewController, each has it's delegate and is calling one or the other. One class is called TopicViewController and the other is MentionViewController, the code looks something like the following:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <RestKit/RestKit.h>
#import "Message.h"
#import "Imgur.h"
#import "URLViewController.h"
#import "CVore.h"
#import "NSData+Base64.h"
#import "Imgur.h"
#import "ProfileViewController.h"
#import "OptionsViewController.h"
#import "Three20/Three20.h"
#class DetailViewController;
#class MentionViewController;
#protocol DetailViewControllerDelegate
- (void) viewController:(DetailViewController*)viewCon withText:(NSString *) text;
#end
#interface DetailViewController : UIViewController <MentionViewControllerDelegate>
///////////////////////////////////////////////////////////////////////////////////
#import <UIKit/UIKit.h>
#import <RestKit/RestKit.h>
#import "Members.h"
#import "DetailViewController.h"
#import "Three20/Three20.h"
#class MentionViewController;
#protocol MentionViewControllerDelegate
- (void) viewController:(MentionViewController*)viewCon withUsername:(NSString *) text;
#end
#interface MentionViewController : UITableViewController <DetailViewControllerDelegate>
Now the problem is that when I add #import "MentionViewController.h" to the DetailViewController it gives me the following error in the MentioViewController:
Cannot find protocol declaration for DetailViewControllerDelegate.
I understand this might be due to cylical referencing, but how do I solve this?
It is really strange. The MentionViewController needs the header file of DetailViewController, and the DetailViewController needs MentionViewController's header file. It is a cycle. Maybe you need to create a empty header file, and put all protocol inside it. For example,
MyProtocol.h
#class DetailViewController;
#class MentionViewController;
#protocol DetailViewControllerDelegate
- (void) viewController:(DetailViewController*)viewCon withText:(NSString *) text;
#end
#protocol MentionViewControllerDelegate
- (void) viewController:(MentionViewController*)viewCon withUsername:(NSString *) text;
#end
And add #import MyProtocol.h inside DetailViewController.h and MentionViewController.h.
You need to use forward declaration for the protocols and only import the headers in the implementation file.
I think your intuition is correct.
You should be able to solve this problem by declaring the 2 protocols in a header file of their own, then import this file from your .m files.
This will break the cycle.

Consequence of importing appDelegate in a class and the same class in appDelegate

I want to know the consequence of importing appDelegate in a class and the same class in appDelegate. Because, I'm doing this in my application successfully, but it's recommended that it should not be done. I couldn't find the answer despite a lot of searching.
Thanks in advance.
You can do it, but be careful with how you import headers. This is the recommended way:
AppDelegate.h:
// Import headers here
#class MyViewController;
#interface AppDelegate : NSObject <UIApplicationDelegate> {
MyViewController *viewController;
}
- (void)someMethod;
#end
AppDelegate.m:
#import "AppDelegate.h"
#import "MyViewController.h"
#implementation AppDelegate
//Your code here
#end
MyViewController.h:
// Import headers here
#class AppDelegate;
#interface MyViewController : UIViewController {
AppDelegate *appDelegate;
}
#end
MyViewController.m:
#import "MyViewController.h"
#import "AppDelegate.h"
#implementation MyViewController
// Your code here
#end
As you can see, you want to use #class to declare the classes in your headers, and then import the header in your .m files. This ensures that you’re not importing things that you don’t need; if you imported the view controller header in your app delegate’s header, it would be imported into anything that imported your app delegate’s header. By leaving all the imports to the .m files, you prevent that situation.