1

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

1
  • The crash is entirely unrelated to the protocols. Ask a different question. I'd bet that you have a 'window' binding in Interface Builder? Commented Feb 11, 2010 at 15:20

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.'
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?
above what i've written is the actual codem and also i've copy - n - pasted the same error
0

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.

5 Comments

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..
Should i import protocol_B and protocol_C also in MyClass.h??
@suse: Your protocol_A header should import protocol_B and protocol_C headers if it extends those protocols.
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... ??
@bbum: Can methods in protocol have return type and parameter passing?
0

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

3 Comments

hey thanks...but stil i'm getting ***ERROR:Expected identifier or '(' before '{' token ***thrice , wats it related to... ??
You have a typo in MyClass.m, @impelemntation should be @implementation.
hey.. no.. in my code its proper.. its only a typo error in code in this forum.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.