Objective C协议用法 [英] Objective C protocols usage

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

问题描述

我有一个困扰我的功课问题,非常糟糕。以下是对问题的简要说明。

I have a homework question which confused me, really badly. Below is a brief explanation of a question.


想象一下,您正在开发一个存储联系人
信息的应用程序。地址簿可以包含许多实体类型,例如人类
是公司或其他任何有联系信息的公司。

Imagine you are developing an application that stores contact information. The address book may contain many entity types e.g. Human being, a company or anything else that has a contact information.


  • 现在而不是显式检查每个对象类型写一个
    协议声明一个对象必须如何表现并成功
    出现在你的地址簿中。






我对回答这个问题的理解和努力是,


My understanding and efforts of answering this question is,


  1. @required 标记下构建一个协议,其中包含每种联系信息的常用方法。在@optional下,所有其他不同的联系方式(例如传真号码与公司有关,但不是人......)。在运行时,您可以使用 selector 检查对象是否响应任何给定方法。
    怀疑:然而,这又是间接明确地检查对象类型,我是对的吗?

  1. Build a protocol which has common methods of each type of contact information under @required tag. And all other methods which are not similar in different contact(Such as fax number has association with company but not person...) under @optional. At runtime you can check whether an object responds to any given method by using selector. Doubt : However this is again explicitly checking object type indirectly, am I right?

我的第二个想法是在java中使用类似抽象类的东西。这意味着抽象类中的继承类实现了自己的抽象方法。作为一个天真的iOS开发人员,我不知道如何实现这个?我不确定这是否能解决我的问题。如果有人知道,我想得到启发。

My second thought is to use something like abstract class in java. Which means inherited class's from abstract class implements their own abstract methods. How ever as a naive iOS developer I don't know how to implement this? and I am not sure whether this is going to solve my problem. I would like get enlighten if someone knows this.






外部到目前为止已完成阅读,如果我正在寻找的答案是在其中一个链接中,请告诉我。我会再次阅读它以了解并解决这个问题:)。谢谢。


External Reading done so far, Please let me know if the answer I am looking for is in one of these links. I will read it again to understand and solve this :). thanks.


  1. http://developer.apple.com/library/mac/#documentation/Cocoa/ Conceptual / ObjectiveC / Chapters / ocProtocols.html#// apple_ref / doc / uid / TP30001163-CH15-TPXREF144

http ://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProtocols.html#//apple_ref/doc/uid/TP30001163-CH15-TPXREF146

http://developer.apple.com/library/mac/#documentation/Cocoa /Conceptual/ObjectiveC/Chapters/ocProtocols.html#//apple_ref/doc/uid/TP30001163-CH15-TPXREF149


推荐答案

协议与Java接口相同。它只定义了类应该支持哪些方法。这是一个清楚解释的页面: http://www.otierney.net/objective- c.html#protocols

A protocol is the same thing as a Java interface. It just defines which methods the class should support. Here's a page that explains it clearly: http://www.otierney.net/objective-c.html#protocols

基本上,如果你想确保一个类有一个 phoneNumber 方法(访问 phoneNumber 属性)你会做这样的事情:

Essentially if you want to make sure a class will have a phoneNumber method (accessor to the phoneNumber property) you would do something like this:

@protocol ContactProtocol
-(void) phoneNumber;
@end

@interface Person: NSObject <ContactProtocol> {
    ...
}

@interface Company: NSObject <ContactProtocol> {
    ...
}

然后在编译时(或直播)对于xcode 4)它会告诉你是否忘记将 phoneNumber 方法添加到 Person 公司类。

And then at compile time (or live for xcode 4) it will tell you if you forgot to add the phoneNumber method to the Person or Company classes.

这篇关于Objective C协议用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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