目标C - 如何创建界面? [英] Objective C - How can I create an interface?

查看:69
本文介绍了目标C - 如何创建界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够像在C#中创建一样创建一个接口来强制执行一组类来实现某些方法。
这在目标c中是否可行?

I need to be able to create an interface like you create in C# to enforce a group of classes to implement certain methods. Is this possible in objective c?

推荐答案

您可以创建协议。它看起来像这样:MyProtocol.h中的

You can create a protocol. it would look something like this: in MyProtocol.h:

@protocol MyProtocol
-(void)myMethod;
-(void)myMethod2;
@end

MyClass.h中的

in MyClass.h

#import "MyProtocol.h"
@interface MyClass : NSObject<MyProtocol>
@end

如果你想接收给定协议的对象,你可以这样做像这样:

If you want to receive objects of a given protocol, you can do so like this:

id<MyProtocol> var;

NSObject<MyProtocol> *var;

更多信息这里

这篇关于目标C - 如何创建界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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