imported class not recognized in delegate file ios 7.0 - objective-c

Here is my code:
//
// mapViewControllerDelegate.h
#import <Foundation/Foundation.h>
#import "mapViewController.h"
#protocol mapViewControllerDelegate <NSObject>
-(void)mapViewControllerClickedDoneButton:(mapViewController*)map;
#end
I get error "unrecognized type", wondering why. Probably something really basic, sorry.

You have to set the delegate property of self to a value

Related

"Expected a Type" error for protocol which should be known to the compiler

Minimum Example "Test.h":
#import <Foundation/Foundation.h>
#protocol CallBack <NSObject>
-(void)method;
#end
#interface Test : NSObject
-(void)callback:(CallBack*)theCallback;
#end
And the corresponding "Test.m":
#import "Test.h"
#implementation Test
-(void)callback:(CallBack*)theCallback
{
[theCallback method];
}
#end
This will give me a "Expected a Type" error for the CallBack parameter both in the .m and the .h file. As the CallBack protocol is defined before everything else, i can't see why the compiler can't find it. If i add a Forward-Definition #class CallBack; at the beginning of the header file it will give me a "Receiver type 'CallBack' for instance message is a forward declaration" error for the line [theCallback method].
why can't the compiler find the protocol?
The correct syntax to refer to an object that conforms to the CallBack protocol is id<CallBack>.
Thus, you might want:
#protocol CallBack <NSObject>
-(void)method;
#end
#interface Test : NSObject
-(void)callback:(id <CallBack>)theCallback;
#end
and
#implementation Test
-(void)callback:(id <CallBack>)theCallback
{
[theCallback method];
}
#end
For more information, see Working with Protocols in the Programming with Objective-C guide.

Xcode warns about missing protocol definition, even though #protocol is used

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

iOS: What can cause the Xcode compiler to throw errors when I include my AppDelegate.h into another header file?

I am trying to #import my "AppDelegate.h" into another header file of the same project in order to access methods of the AppDelegate of my iOS project.
So, my header file looks something like this:
#import
#import "DataProvider.h"
#import "MyAppDelegate.h"
#interface MyViewController : UIViewController <UITextFieldDelegate, UIAlertViewDelegate> {
...
}
and I want to use my AppDelegate like this:
MyAppDelegate* appDelegate = (MyAppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate doSomething];
However, as soon as I #import "MyAppDelegate.h", the compiler throws a lot of (unrelated) errors, like
Cannot find interface declaration for 'MyOtherViewController', superclass of 'MyOtherViewController2'
The thing is: I can include my AppDelegate just fine in other headers. And I cannot figure out what the difference might be. Please help me figure out what could cause this! Thanks a lot!
PS: This happens with GCC as well as the new LLVM.
Move the #import into the .m file. If you need the MyAppDelegate symbol in your .h, use #class MyAppDelegate; instead.

Creating Delegate methods in a Protocol

I cannot seem to build my protocol the way I would like and I have narrowed down to a problem with using derived classes. If I use a cocoa class it seems to work. Here is what I have...
#import <Foundation/Foundation.h>
#import "MyView.h"
#protocol MyDelegate
- (void)view:(MyView *)aView didDoSomethingWithString:(NSString *)string;
#end
The MyView class is...
#import <UIKit/UIKit.h>
#interface MyView : UIView {
NSString *whatever;
}
- (void)myMethod;
#end
#implementation MyView
- (void)myMethod {
doSomething...
}
#end
So when I attempt to build I get the error "Expected ')' before 'MyView'". If I replace the custom class MyView with UIView then the code compiles. I am hoping someone sees something that I am overlooking. Any ideas are appreciated.
Thanks.
Are you sure MyView.h contains #interface MyView : UIView?
Also, instead of importing you can use #class. e.g.
#class MyView;
#protocol MyDelegate
- (void)view:(MyView *)aView didDoSomethingWithString:(NSString *)string;
#end
Try putting the #interface and #implementation parts in different files (if you currently have them in the same file). It looks like you have all that in MyView.m, and you're importing MyView.h, which doesn't exist.

#protocol implementation in #interface in Objective-C

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.