Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-14-2010, 07:00 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 13
Dr.Lightning is on a distinguished road
Default Cannot find protocol declaration, duplicate declaration, delegate

I have some wierdness going on that I don't understand. Perhaps its some "import" feature I don't understand. Perhaps it's cuz it's February (sarcasm).

Derived from the "first iPhone application" hello program, I have the following pertinent code:

ControlPanel.h:

...

#import <UIKit/UIKit.h>
#import "MainViewController.h"
#import "Connection.h"

@protocol ControlDelegate

- (BOOL)sendSetPoint: (float)setPoint Group: (int)groupNumber;

@end

...
mainViewController.h:

...

#import <UIKit/UIKit.h>
#import "ControlPanel.h"

#ifdef SeeForumText
@protocol ControlDelegate

- (BOOL)sendSetPoint: (float)setPoint Group: (int)groupNumber;

@end
#endif

@interface MainViewController : UIViewController <UITextFieldDelegate, DisplayDelegate, UIScrollViewDelegate> {

// Declare Display Delegate
id<ControlDelegate> controlDelegate;

...
Note that I've identified some lines above with #ifdef SeeForumText.

When those lines are ABSENT, as I believe SHOULD be the case, I get an error message on the id...controlDelegate line that reads "Cannot find protocol declaration for 'ControlDelegate'"

When those lines are PRESENT, I get a warning message within them that reads "Duplicate declaration for protocol 'ControlDelegate'. I make no other changes whatsoever.

It seems I'm d*mned if I do, and d*mned if I don't. This makes no sense to me. I've been moving forward with the warning, because it allows me to make progress on my development. But, at some point, I need to figure this out and get rid of the warning.

I see the circular import of .h files, but it's necessary because they both reference each other. I thought import took care of that. But am I getting this as a side effect?

Your help is greatly appreciated.
Dr.Lightning is offline   Reply With Quote
Old 02-14-2010, 07:06 PM   #2 (permalink)
ole
Registered Member
 
ole's Avatar
 
Join Date: Dec 2009
Location: Berlin
Posts: 14
ole is on a distinguished road
Default

You should remove the duplicate protocol declaration and yes, I'd say the circular import of the .h files is the problem. Instead of importing MainViewController.h in ControlPanel.h, just add a forward declaration of the class(es) you need to reference to the file:

Code:
// ControlPanel.h
#import <UIKit/UIKit.h>
#import "Connection.h"

@class MainViewController;

@protocol ControlDelegate
...
ole is offline   Reply With Quote
Old 02-15-2010, 01:18 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 13
Dr.Lightning is on a distinguished road
Default

Yep. It was the recursive imports.

I checked my reference example code, and it put each delegate declaration in a separate .h file, such as ControlDelegate.h. I suspect there was NO RESOLUTION to the recursion, without so separating the delegate declarations. I did this for my three delegates, and the problem went away.

The problem arose from having three logical layers in my app, and having each layer communicate with the other through a delegate. Each adjacent layer communicates with the next, and this led to the recursion. Therefore, each class had to include the definition of the other.

For classes, one can forward reference via "@class name;". However, I tried this for "@protocol name;" and it appears that the forward reference doesn't work. Therefore, I had to go the separate delegate declaration route.

Now, all layers that reference a delegate have a .h file that imports the delegate declaration .h file. The import's "don't double include" feature now does what it's supposed to do, because in the end these delegate declaration .h files are indeed included multiple times. But not circularly.

From now on, I'll just make it a standard of putting the delegate declarations in a separate .h. Again, I believe this is necessary because "@protocol name;" fails to act as a forward reference in the same way that "@class name;" does.

If I'm mistaken about the above, it ought to be a matter of syntax, because the exact text "@protocol name;" doesn't help. If I'm mistaken and somebody points out the correct, I *might* try. But then, the separate file solution is a fine one as well.
Dr.Lightning is offline   Reply With Quote
Old 02-17-2010, 09:55 AM   #4 (permalink)
ole
Registered Member
 
ole's Avatar
 
Join Date: Dec 2009
Location: Berlin
Posts: 14
ole is on a distinguished road
Default

Quote:
Originally Posted by Dr.Lightning View Post
For classes, one can forward reference via "@class name;". However, I tried this for "@protocol name;" and it appears that the forward reference doesn't work.
This is not true. Forward declaration of protocols work just like forward declarations of classes.
ole is offline   Reply With Quote
Old 02-19-2010, 01:43 PM   #5 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 13
Dr.Lightning is on a distinguished road
Default

Quote:
Originally Posted by ole View Post
This is not true. Forward declaration of protocols work just like forward declarations of classes.
Acknowledged. Dunno why it didn't work for me. Either way, I'll stick with putting the delegate decl in separate .h files. Thanks for your help.
Dr.Lightning is offline   Reply With Quote
Old 08-16-2010, 06:16 PM   #6 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 16
cynistersix is on a distinguished road
Default

Quote:
Originally Posted by Dr.Lightning View Post
Acknowledged. Dunno why it didn't work for me. Either way, I'll stick with putting the delegate decl in separate .h files. Thanks for your help.
Ran into something similar had to do with the protocol being in a header file and #include'ing it instead of #import'ing it. Watch out for this zinger.
cynistersix is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 331
5 members and 326 guests
2Apps1Day, akacaj, SLIC, soohyun, Techgirl-52
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,115
Posts: 402,887
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:07 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0