`
mmdev
  • 浏览: 12889777 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

objective-c中interface与protocol的作用

 
阅读更多

以前对objective-c中的interface,即头文件的作用一直不太清楚。最近看了一些文章,再加上自己的试验,对头文件的作用稍有了解。

在我看来,头文件的作用是,定义对外的接口。

然而,它的作用也只有这个而已。头文件无法保证对外接口一定会被实现。

根据.h文件是否定义方法、.m文件是否实现方法,可以分为三类:

第一类是.h文件定义方法,.m文件也实现了方法,这是最common的做法,也是最没有问题的做法。

第二类是.h文件定义方法,但.m文件没有实现该方法。此时.m文件的@implementaion代码会提示“imcomplete implementation”,意思是头文件定义的方法和变量,没有被完全实现。

比如:.h文件里这样定义:

@interface Test : NSObject{}

-(void)hello;

@end
但是在.m文件里没有实现这个方法。在外部,我们可以调用这个方法,它在编译时没问题,但在运行时会出现“unrecognized selector sent to instance”错误。

第三类是.h文件没有定义,但.m文件里有这个方法。

比如,在.m文件里这样实现:

#import "Test.h"

@implementation Test

-(void)hello{
    NSLog(@"hello world!");
}

@end
这时,hello()这个函数相当于私有函数,我们只可以在类中用[self hello]调用,而不能在外部调用。

因此我觉得,objective-c中的头文件,只是为了编译时更方便而已,它并不是真正的interface。

相对而言,protocol才算是真正意义上的interface,它的意义和Java中的接口差不多。

protocol的方法分两种类型,一类是必须实现的,一类是不一定实现的。不一定实现的方法其实和.h文件里定义的差不多。而一定要实现的方法就是比较有用的了。

我们可以使用这样的方法来初始化一个实现了名为Hello的protocol的类:

id<Hello> test = [[Test alloc] init];

protocol除了作为接口的作用之外,经常还用来作为类之间进行交互的方法,这时它叫作delegate。delegate的主要作用,是把一个类需要做的一部分事情,让另一个类来完成。网上介绍delegate的文章铺天盖地,这里不再说述。

分享到:
评论

相关推荐

    详解 objective-c中interface与protocol的作用

    主要介绍了详解 objective-c中interface与protocol的作用的相关资料,需要的朋友可以参考下

    Objective-C解析器:获取Objective-C头文件的JSON表示形式

    获取Objective-C头文件并将其转换为等效的javascript调用 安装 $ npm install objective-c-parser 用法 const fs = require ( "fs" ) ; const objectiveCParser = require ( "objective-c-parser" ) ; const ...

    OBJECTIVE-C编程之道 IOS设计模式解析电子书+源代码

     《Objective-C编程之道:iOS设计模式解析》适用于那些已经具备Objective-C基础、想利用设计模式来提高软件开发效率的中高级iOS开发人员。第一部分 设计模式初体验第1章 你好,设计模式1.1 这是一本什么书1.2 开始...

    Programming in Objective-C 4th Edition

    An Objective-C Class for Working with Fractions 30 The @interface Section 33 Choosing Names 34 Class and Instance Methods 35 The @implementation Section 37 The program Section 39 Accessing Instance ...

    GenericModel:Objective-C Model、JSON、NSDictionary互相转换简单高效的轻量级框架,支持model嵌套model

    GenericModel 支持Objective-C Model、NSDictionary、JSON之间互相转换,框架非常简单高效,内部对反射过的model设置有缓存,用Objective-C中的Protocol限定NSArray,NSDictionary等容器类的类型,防止容器类型变量...

    objective-c的基本知识

    Category是ObjC语言中的扩展机制之一,另一个为Protocol。 Category提供一种为某个类添加方法而又不必编写子类的途径。 假设有这样一个类CarInfo: #import @interface CarInfo : NSObject { } -(void)sayCarBrand;...

    Cocoa Fundamentals Guide

    The Dynamism of Objective-C 57 Extensions to the Objective-C Language 58 Using Objective-C 64 The Root Class 66 NSObject 67 Root Class—and Protocol 67 Overview of Root-Class Methods 68 Interface ...

    class-dump-3.5.tar

    用来dump目标文件的class信息的工具。它利用Objective-C语言的runtime的特性,将存储在mach-O文件中的@interface和@protocol信息提取出来,并生成对应的.h文件。

    基于protocol的iOS模块路由和依赖注入框架.zip

    基于protocol的iOS模块路由和依赖注入框架.zip,Interface-oriented router for discovering modules, and injecting dependencies with protocol in Objective-C and Swift.

    USB30 Universal Serial Bus 3.0 Specification pdf it is free

    C语言程序设计(Visual+C 6.0环境) http://download.csdn.net/source/2232878 Visual C++ 60 MFC + code 学习最强宝典 http://download.csdn.net/source/2236266 ASP.NET Web (第一次亲密接触ASP.NET) ...

    iOS 9 Programming Fundamentals with Swift 无水印pdf 0分

    Find out how Swift communicates with Cocoa’s C and Objective-C APIs Once you master the fundamentals, you'll be ready to tackle the details of iOS app development with author Matt Neuburg's companion...

    iOS 9 Programming Fundamentals with Swift(O'Reilly,2015)

    Find out how Swift communicates with Cocoa’s C and Objective-C APIs Once you master the fundamentals, you’ll be ready to tackle the details of iOS app development with author Matt Neuburg’s ...

    iPhone Cool Projects

    It moves on to Rogue Amoeba's Mike Ash explaining how to design a network protocol using UDP, and demonstrating its use in a peer-to-peer application—a topic not normally for the faint of heart, but...

    BHObjection:反对演示

    Spring中通过 java 的标注,实现对 module 的注入,在 OC 中需要手写 Module, 然后在类方法+load注入 module 建立起 Class 和 Interface 之间的关系,当使用一个类的时候,通过接口去实例化. ##注入 &lt;pre&gt;&lt;code&gt; +(void)...

    injector:iOS 应用的服务注入器

    @protocol HelloService - ( void ) sayHello ; @end 以及上述的实现 // HelloNSLogger.h @interface HelloNSLogger @end // HelloNSLogger.m @implementation HelloNSLogger - ( void ) sayHello { NSLog ( @...

    UpDownDraggingTable

    头文件如下所示: @protocol DataUpdateCallback // 定义一个协议,调用者需要提供数据@必需的(int) updateData: (BOOL) 标头; @结尾@interface KRefreshTableView : UITableView { EGORefreshTableHeaderView *_...

    DebuggingWithGDB 6.8-2008

    Table of Contents Summary of gdb . . . . . . . . ....Free Software ....Free Software Needs Free Documentation ....Contributors to gdb....1 A Sample gdb Session ....2 Getting In and Out of gdb ....2.1 Invoking gdb ....

    BFEmptyDataSet:空视图处理

    BFEmptyDataSet 参考 经过修改 Features 无网络状态,无数据状态,空视图处理 支持所有UIView Installation ...@interface ViewController : UIViewController - (void)viewDidLoad { [super viewDidLoa

    Debugging with GDB --2003年6.0

    Table of Contents Summary of gdb . . . . . . . . ....Free software ....Free Software Needs Free Documentation ....Contributors to gdb....1 A Sample gdb Session ....2 Getting In and Out of gdb ....2.1 Invoking gdb ....

Global site tag (gtag.js) - Google Analytics