开发者

@protocol implementation in @interface in Objective-C

开发者 https://www.devze.com 2022-12-20 00:46 出处:网络
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.

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 proto开发者_JS百科col_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.

0

精彩评论

暂无评论...
验证码 换一张
取 消