views:

384

answers:

3

Hello, 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??

A: 

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.

bbum
hi,... i've edited my code above, i've done #import <protocol_A> in MyClass.h file., since tat is the only protocol i'm implementing in MyClass, because inturn it extends other protocol_B and protocol_C, i'll be defining all methos in MyClass. i'm not ablt to find out what the mistake,. plz help..
suse
Should i import protocol_B and protocol_C also in MyClass.h??
suse
@suse: Your `protocol_A` header should import `protocol_B` and `protocol_C` headers if it extends those protocols.
dreamlax
hey thanks.. i knw it was a silly mistake.. :P... but stil i'm getting ***ERROR:Expected identifier or '(' before '{' token ***thrice , wats it related to... ??
suse
@bbum: what can be the reason to get these error??
suse
@bbum: Can methods in protocol have return type and parameter passing?
suse
A: 

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
Rob Keniger
hey thanks...but stil i'm getting ***ERROR:Expected identifier or '(' before '{' token ***thrice , wats it related to... ??
suse
You have a typo in `MyClass.m`, `@impelemntation` should be `@implementation`.
Rob Keniger
hey.. no.. in my code its proper.. its only a typo error in code in this forum.
suse
+1  A: 

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.

CajunLuke
Hey.. super!!! thanks ... :) i'm getting this Exception regarding, ***Terminating app due to uncaught excception 'NSUnknownKeyException', reason: '[<MyClass 0X231nm21> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key window.'
suse
You'll need to post your actual code to try to solve this. Could you copy-and-paste the exact error message you see, too?
CajunLuke
above what i've written is the actual codem and also i've copy - n - pasted the same error
suse