Cannot find protocol declaration for "XXXX" - objective-c

I am stuck with this problem. I have the following .h file:
#import <UIKit/UIKit.h>
#protocol MapSettingsViewDelegate
- (void)settingsDidUpdate:(BOOL)scheme;
#end
#interface MapSettingsViewController : UIViewController
#property (nonatomic, assign) id <MapSettingsViewDelegate> delegate;
#property (nonatomic, strong) IBOutlet UINavigationBar *navBar;
#property (nonatomic, strong) IBOutlet UINavigationItem *titleItem;
#property (nonatomic, strong) IBOutlet UITableView *TableView;
- (id)init;
- (IBAction)saveAction:(id)sender;
#end
When I declare the following:
#interface MapViewController : UIViewController <MapSettingsViewDelegate>
The compiler complains with the following message:
Cannot find protocol declaration for 'MapSettingsViewDelegate'
I have the same kind of delegate declaration in other files in the same project that are compiled without a glitch. I spent the last four hours trying to figuring out what I am doing wrong. Cleaning the project does not nothing.

Problem solved. As suggested in this answer, I created a new class with a different name and copied all coded from the previous class. Worked flawlessly. It seems XCode lost track of something.

You are running into preprocessor problems. Both of your classes import each other. I think you can solve this by adding #class below your protocol declaration as shown below; and moving your #import "mapViewController.h" line into MapSettingsViewController.m file. The #class tells the compiler that you will include the class content somewhere else (in the .m file).
Note that, on the top of your MapViewController.h class file you can include #import "MapSettingsViewController.h" just like normal. Hope this help. I ran into the same problem myself earlier.
#import <UIKit/UIKit.h>
#protocol MapSettingsViewDelegate
- (void)settingsDidUpdate:(BOOL)scheme;
#end
#class MapViewController
#interface MapSettingsViewController : UIViewController
#property (nonatomic, assign) id <MapSettingsViewDelegate> delegate;
...
...

Import the MapSettingsViewController.h file

Related

incompatible with type id<NSTextFieldDelegate>

I am getting new warning on my old OSX app. I am using OSX 10.10 and I am not quite sure where the problem is. Can someone help?
The actual warning is Property type 'id is incompatible with type id inherited from NSTextField
#import <Cocoa/Cocoa.h>
#import "HyperlinkTextFieldDelegate.h"
#interface HyperlinkTextField : NSTextField <NSTextFieldDelegate>
#property (assign) id <HyperlinkTextFieldDelegate> delegate; <--- warning showing up here
#end
The main implementation is
#interface HyperlinkTextField ()
#property (nonatomic, readonly) NSArray *hyperlinkInfos;
#property (nonatomic, readonly) NSTextView *textView;
- (void)_resetHyperlinkCursorRects;
#end
#define kHyperlinkInfoCharacterRangeKey #"range"
#define kHyperlinkInfoURLKey #"url"
#define kHyperlinkInfoRectKey #"rect"
#implementation HyperlinkTextField
#synthesize delegate;
And the delegate file is
#import <Foundation/Foundation.h>
#protocol HyperlinkTextFieldDelegate <NSObject>
- (void) barLinkClicked: (id) sender;
#end
NSTextField already has a delegate property, and it is typed as id<NSTextFieldDelegate>. Thus, your HyperinkTextField, which is a subclass of NSTextField, inherits this property, just as the error message clearly tells you. You cannot override this inherited property and type it as id<HyperlinkTextFieldDelegate> where that is a different type.

Objective C forward declaration and type compatibility

I have code like following:
#import "ViewModelBase.h"
#interface ViewControllerA : UIViewController
#property (nonatomic, strong) ViewModelBase* viewModel;
#end
Then I need to do something like following:
#import "ViewControllerA.h"
#class ViewModelSubclass;
#interface ViewControllerB : ViewControllerA
#property (nonatomic, strong) ViewModelSubclass* viewModel;
#end
Where ViewModelSubclass.h is not supposed to be imported to ViewControllerB.h, only to .m file.
However, ViewModelSubclass is supposed to be subclass of ViewModelBase.
This is done to hide interface of ViewModelSubclass from clients of ViewControllerB.
How do I solve "Property type 'ViewModelSubclass*' is incompatible with type 'ViewModelBAse*' inherited from ViewControllerA" warning?
Or what is a better way to do it?
don't use like this
#import<ViewControllerA.h>
try to use like
#import "ViewControllerA.h"
I think it will solve your problem

xcode objective c missing #end on NSObject

I've been racking my brains for the past 2 hours regarding this issue and I couldn't find any solution through searching or by removing the imported files of my CustomTableCell.
Here's my class (.h)
#import <UIKit/UIKit.h>
#interface MatchTableCell : UITableViewCell{
}
#property (nonatomic, weak) IBOutlet UILabel *matchId;
#property (nonatomic, strong) IBOutlet UILabel *fighter1Name;
#property (nonatomic, weak) IBOutlet UILabel *fighter2Name;
#property (nonatomic, weak) IBOutlet UILabel *status;
#end
This is what's popping up:
Missing '#end' .. this is popping up on the #interface line
Expected Identifier or '(' .. this highlights the first property
Thoughts?
EDIT: Solution. Apparently I can't post this as an answer even though that's how I solved the issue. Anyway, just in case it helps anyone, check the imports of the ViewControllers (or objects) that imports the file getting the error
Well, that was very tedious. Found a ViewController that is imported by a second viewcontroller, this second viewcontroller imports this NSObject. The first viewcontroller, for some reason got changed from UITableViewController to UIViewController. Thanks to #rob-mayoff for the idea
You are missing the #end directive at the end of one of your other .h files. Go through the .h files in your project one by one. For each one that has an #interface or #protocol section, make sure the section is terminated with an #end directive.
Also to mention that start with checking the recently added files. XCODE starts to show this error any where but doesn't indicate which file is missing the combination of #interface and #end. One of the file will have it missing.

iOS 5.0 Warning: Cannot find protocol definition for Delegate

I have custom UIView class GestureView. I have a forward declaration for this class and it's delegate below. I have imported GestureView.h in .m file. This works fine but iOS gives warning message saying "Cannot find protocol definition for GestureViewDelegate". If I remove forward declaration it gives same warning message as error. I don't want to import GestureView.h from ContainerViewController.h as I usually imports stuffs in .m file. Could someone please explain what's wrong in following class structure?
ContainerViewController.h
#import <UIKit/UIKit.h>
#class DividerView;
#class GestureView;
#protocol GestureViewDelegate;
#interface ContainerViewController : UIViewController<GestureViewDelegate>
#property (strong, nonatomic) IBOutlet GestureView *topContentView;
#end
GestureView.h
#import <UIKit/UIKit.h>
#protocol GestureViewDelegate;
#interface GestureView : UIView
- (void)initialiseGestures:(id)delegate;
#end
#protocol GestureViewDelegate <NSObject>
#required
- (void)GestureView:(GestureView*)view handleSignleTap:(UITapGestureRecognizer*)recognizer;
#end
I like that you're trying to avoid imports in header files: very good practice. However, to fix your bug you can just make your code even better! In my opinion it's not really necessary that your ContainerViewController class outwardly declares that it supports GestureViewDelegate protocol, so you should move this into your implementation file. Like so:
GestureView.h
#import <UIKit/UIKit.h>
#protocol GestureViewDelegate;
#interface GestureView : UIView
- (void)initialiseGestures:(id <GestureViewDelegate>)delegate;
#end
#protocol GestureViewDelegate <NSObject>
#required
- (void)gestureView:(GestureView *)view handleSingleTap:(UITapGestureRecognizer *)recognizer;
#end
ContainerViewController.h
#import <UIKit/UIKit.h>
#class GestureView;
#interface CollectionViewController : UIViewController
// this property is declared as readonly because external classes don't need to modify the value (I guessed seen as it was an IBOutlet)
#property (strong, nonatomic, readonly) GestureView *topContentView;
#end
ContainerViewController.m
#import "ContainerViewController.h"
#import "GestureView.h"
// this private interface declares that GestureViewDelegate is supported
#interface CollectionViewController () <GestureViewDelegate>
// the view is redeclared in the implementation file as readwrite and IBOutlet
#property (strong, nonatomic) IBOutlet GestureView *topContentView;
#end
#implementation ContainerViewController
// your implementation code goes here
#end
Try this way, and please reply if it works or not.
GestureView.h
#import <UIKit/UIKit.h>
#protocol GestureViewDelegate <NSObject>
#required
- (void)GestureView:(GestureView*)view handleSignleTap:(UITapGestureRecognizer*)recognizer;
#end
#interface GestureView : UIView
- (void)initialiseGestures:(id)delegate;
#end
ContainerView.h
#import <UIKit/UIKit.h>
#class DividerView;
#class GestureView;
/*#protocol GestureViewDelegate;*/ //NO NEED TO WRITE THIS
#interface ContainerViewController : UIViewController<GestureViewDelegate>
#property (strong, nonatomic) IBOutlet GestureView *topContentView;
#end

unknown type name in objective c

I'm pretty new to objective c, and having some basic problems.
I wrote a simple program using a navigator, and everything worked fine.
then I added few lines of code (can't even remember what exactly, and it seems to have no connection to the problem) and the problem occurred. I tried ctrl+z, and the problem remained:
I run the program and get these errors:
1. unknown type name "mainController"
2. property with 'retain (or strong)' attribute must be of object type
while mainController is the first screen to be loaded.
This is the appDelegate.h file:
#import <UIKit/UIKit.h>
#import "mainController.h"
#import "WishesList.h"
#import "Wish.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) IBOutlet UINavigationController *navController;
#property (strong, nonatomic) IBOutlet mainController *viewController; // this line creates the errors
#property (strong, nonatomic) WishesList *WishesArray;
#property (strong, nonatomic) NSIndexPath *temp;
#end
this is the relevant part of the appDelegate.m file:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
WishesArray = [[WishesList alloc]init];
temp = nil;
[self setViewController:[[mainController alloc]init]];
[self setNavController:[[UINavigationController alloc]initWithRootViewController:self.viewController]];
[self.window setRootViewController:navController];
[self.window makeKeyAndVisible];
return YES;
}
And this is mainController.h:
#import <UIKit/UIKit.h>
#import "addWishController.h"
#import "displayWish.h"
#import "WishesList.h"
#import "Wish.h"
#interface mainController : UIViewController
#property (nonatomic, weak) WishesList *list;
#property (nonatomic, strong) IBOutlet UITableView *wishTable;
- (void)addWish;
#end
it already worked...
can you figure it out?
thanks
I figured out, that the same error appears if you have an import-cycle:
Class_A.h: #import "Class_B.h"
Class_B.h: #import "Class_A.h"
To fix: look for any imports of the offending class (the error tab is your friend, expand the relevant error for a list of imports). Remove #import's accordingly
This problem happen to me once.
I was importing the "APPDelegate.h" in my h file and in my APPDelegate.h I was importing the file too (it shouldn't be a problem but...)
What I did: I changed the Import from my own .h to .m and it worked :)
As others have mentioned, this is indeed caused by cyclic imports. To fix this remove the imports in one of the classes. But sometimes this is not sufficient. If the classes depend on each other, simply forward-declare the one class in the other:
Class A:
#import <UIKit/UIKit.h>
#class B; //<- this is essential here
#interface A: NSObject
#property(nonatomic, strong) B *b;
//...
In class B we have:
#import "A.h"
#interface B: NSObject
#property(nonatomic, strong) A *a;
#JustAStranger and #NathanielSymer, both are correct!
Anyway, worth remember that this case, below, has the same problem too:
Class_A.h: #import "Class_B.h"
Class_B.h: #import "Class_C.h"
Class_C.h: #import "Class_A.h"
This problem reveal to us how important is take care about owners at our Class relationships. Is very easy creates cycle problems using ObjC headers.
Check the target and the files it is compiling. Perhaps mainController has some how been removed from that target. If so, when building, you would get the message that it cannot be found.
This problem looks like a typo because class names usually start with an uppercase character. Therefore, mainController could/should be MainController. Check the class name to see if the error is indeed a typo, because the compiler is telling you it cannot find any class called mainController.