Objective-C中@interface的@protocol实现 [英] @protocol implementation in @interface in Objective-C

查看:103
本文介绍了Objective-C中@interface的@protocol实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要开发一个应用程序,它有一个实现3个协议方法的接口。
假设协议A扩展了协议B和协议C,接口实现了协议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 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电话我如何解决这个问题??

Plz Tel me how to solve this problem??

推荐答案

所以你从中得到这个(以及你得到它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.

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

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

希望这有帮助!

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

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

这篇关于Objective-C中@interface的@protocol实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆