Objective-C中@interface的@protocol实现

时间:2022-09-07 08:30:20

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,

我需要开发一个应用程序,它有一个实现3个协议方法的接口。假设协议A扩展了协议B和协议C,接口实现了协议A.这就是我的代码的外观,

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

Plz电话我怎么解决这个问题?

4 个解决方案

#1


2  

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:

那么你从哪里获得这个(以及你得到它3次的原因)是你的协议定义中的错误。你有:

//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:

在制作新文件时,我总是通过Xcode的新类文件过程,因为它经常为您提供许多方便的东西。以下是来自Xcode的新protocol_D声明的内容:

#import <Cocoa/Cocoa.h>

@protocol protocol_D


@end

Hope this helps!

希望这可以帮助!

TL;DR: Protocol definitions can't have curly-braces anywhere in them.

TL; DR:协议定义不能在其中的任何位置使用大括号。

#2


0  

Protocols generally go in a .h file; always go in a .h file if you plan on using them anywhere.

协议通常放在.h文件中;如果您计划在任何地方使用它们,请始终使用.h文件。

Just like everything else, you need to #import the .h file that contains the definition of the protocol before you use it.

就像其他所有内容一样,您需要在使用之前#import包含协议定义的.h文件。

So, in MyClass.h (it really should be capitalized -- Classes are always capitalized in Objective-C), #import the various protocol .h files.

因此,在MyClass.h中(它确实应该大写 - 类总是在Objective-C中大写),#import各种协议.h文件。

#3


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:

您的protocol_A.h文件声明符合protocol_B和protocol_C,但您尚未导入protocol_B和protocol_C的标头。这意味着您声明符合协议,就编译器而言,在protocol_A.h中不存在。您需要导入标题:

In protocol_A.h:

在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

#4


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.

另请参阅Apple的Communicating with Objects,它讨论了委托,协议和选择器。虽然它在Mac OS X下列出,但大多数(如果不是全部)似乎也适用于iOS。

#1


2  

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:

那么你从哪里获得这个(以及你得到它3次的原因)是你的协议定义中的错误。你有:

//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:

在制作新文件时,我总是通过Xcode的新类文件过程,因为它经常为您提供许多方便的东西。以下是来自Xcode的新protocol_D声明的内容:

#import <Cocoa/Cocoa.h>

@protocol protocol_D


@end

Hope this helps!

希望这可以帮助!

TL;DR: Protocol definitions can't have curly-braces anywhere in them.

TL; DR:协议定义不能在其中的任何位置使用大括号。

#2


0  

Protocols generally go in a .h file; always go in a .h file if you plan on using them anywhere.

协议通常放在.h文件中;如果您计划在任何地方使用它们,请始终使用.h文件。

Just like everything else, you need to #import the .h file that contains the definition of the protocol before you use it.

就像其他所有内容一样,您需要在使用之前#import包含协议定义的.h文件。

So, in MyClass.h (it really should be capitalized -- Classes are always capitalized in Objective-C), #import the various protocol .h files.

因此,在MyClass.h中(它确实应该大写 - 类总是在Objective-C中大写),#import各种协议.h文件。

#3


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:

您的protocol_A.h文件声明符合protocol_B和protocol_C,但您尚未导入protocol_B和protocol_C的标头。这意味着您声明符合协议,就编译器而言,在protocol_A.h中不存在。您需要导入标题:

In protocol_A.h:

在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

#4


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.

另请参阅Apple的Communicating with Objects,它讨论了委托,协议和选择器。虽然它在Mac OS X下列出,但大多数(如果不是全部)似乎也适用于iOS。