Objective-C 之Extension

class extension:类扩展

类扩展与 category 有相似性,但在编译时它只能被添加到已有源代码的一类中(该类扩展和该类同时被编译)。

  • 在extension里面只能写定义,不能写实现。
  • 通过扩展可以控制默写函数是否堆外界可见。
  • 扩展还可以定义属性(@property)
示例程序:

Student.h

#import <Foundation/Foundation.h>

typedef enum : NSUInteger {
Female,
Male,
} Gender; @interface Student : NSObject -(void)sayHello; @property (readonly) Gender gender; @end

Student.m


#import "Student.h"
#import "Student_Ext.h"
@implementation Student
-(void)sayHello{
NSLog(@"Student say hello");
self.gender = Female;
}
-(void)sayHi{
NSLog(@"Student say Hi");
}
@end

Student_SayHi.h

#import "Student.h"

@interface Student ()
-(void)sayHi;
@property (readwrite)Gender gender; @end

main.m

#import <Foundation/Foundation.h>
#import "Student.h"
#import "Student_Ext.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *s =[[Student alloc] init];
[s sayHello];
[s sayHi];
}
return 0;
}

category和extension

category是用来扩展累的功能的,而extension仅仅扩展定义,没有源代码的话是不能使用扩展的。

参考资料:
自定义现有的类 - Customizing Existing Classes
类扩展(class extension)

Objective-C 之Extension的更多相关文章

  1. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  2. iOS开发——技术精华Swift篇&Swift 2.0和Objective-C2.0混编之第三方框架的使用

    swift 语言是苹果公司在2014年的WWDC大会上发布的全新的编程语言.Swift语言继承了C语言以及Objective-C的特性,且克服了C语言的兼容性问题.Swift语言采用安全编程模式,且引 ...

  3. MD5 algorithm in Objective C

    How to calculate the MD5 in objective C ? md5 is available on the iPhone and can be added as an exte ...

  4. iOS开发核心语言Objective C —— 全部知识点总结

    本分享是面向有意向从事iOS开发的伙伴及苹果产品的发烧友,亦或是已经从事了iOS的开发人员,想进一步提升者.假设您对iOS开发有极高的兴趣,能够与我一起探讨iOS开发.一起学习,共同进步.假设您是零基 ...

  5. Objective -C Categories

    Objective -C Categories  The dynamic runtime dispatch mechanism employed by Objective-C lets you add ...

  6. 关于Objective C的私有函数

    (1)很多从其他语言(例如C++)转到objective c的初学者,往往会问到一个问题,如何定义类的私有函数?这里的“私有函数”指的是,某个函数只能在类的内部使用,不能在类的外部,或者派生类内部使用 ...

  7. Latent Representation Learning For Artificial Bandwidth Extension Using A Conditional Variational Auto-Encoder

    博客作者:凌逆战 论文地址:https://ieeexplore.ieee.xilesou.top/abstract/document/8683611/ 地址:https://www.cnblogs. ...

  8. 论文翻译:2018_Artificial Bandwidth Extension with Memory Inclusion using Semi-supervised Stacked Auto-encoders

    论文地址:使用半监督堆栈式自动编码器实现包含记忆的人工带宽扩展 作者:Pramod Bachhav, Massimiliano Todisco and Nicholas Evans 博客作者:凌逆战 ...

  9. Windbg Extension NetExt 使用指南 【2】 ---- NetExt 的基本命令介绍

    摘要 : 本章节介绍NetExt常用的命令. 并且对SOS进行一些对比. NetExt的帮助 要想玩好NetExt, 入门就得看帮助. 看NetExt的帮助可以调用!whelp 命令. 这样hi列举出 ...

  10. Windbg Extension NetExt 使用指南 【1】 ---- NetExt 介绍

    摘要 : 在使用WINDBG做debugging的时候,需要一个好的工具帮助进行数据分析. 最常见的extension包括SOS, PSSCOR.  NetExt则是另外一种提供了丰富命令功能的deb ...

随机推荐

  1. CodeIgniter问题:Unable to load the requested file: .php

    调试时出现 Unable to load the requested file: .php, 后来排查到是模板渲染的问题,view函数的参数没接收到,修改后就好了.

  2. 加载selenium库

    一.maven的下载.解压以及环境变量配置 1.下载maven: 官网下载地址:http://maven.apache.org/download.cgi 在Files下面下载对应的maven版本(官网 ...

  3. php开启短标签与<?xml version="1.0" encoding="UTF-8"?>冲突

    前两天写了个生成mapsite的源码,想提交到百度和谷歌,在本地测试一点问题都没有,但是在服务器上就显示500错误 最后废了九牛二虎之力,终于可以设置iis直接显示出错信息. 也搞懂了问题所在,默认服 ...

  4. 使用java NIO及高速缓冲区写入文件

    byte[] bytes = Files.readAllBytes(Paths.get("E:\\pdf\\aaa\\html\\text.txt").normalize()); ...

  5. Cannon 60D 电池卡在电池槽了,拔不出来怎么办?

    事情是这样的,本来好好的电池在电池槽里的,后来拿去充电了,充满后就准备装回去,然后一个不小心,电池掉地上了,就看了一下没摔爆,所以也没特别留意有没有什么地方摔坏摔瘸角,然后就往相机里塞,突然就发现塞不 ...

  6. 合并多个tensorflow模型的办法

    直接上代码: import tensorflow as tf from tensorflow.python.tools import freeze_graph from tensorflow.pyth ...

  7. 【编程开发】MD5和RSA

    MD5和RSA是网络传输中最常用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. (1)MD5 MD5(单向散列算法)的全称是Message-Di ...

  8. 【Matlab开发】函数bsxfun的使用

    [Matlab开发]函数bsxfun的使用 标签:[Matlab开发] 版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/lg1259156776/. 说明:当我们 ...

  9. js统计页面中各个标签出现的次数,倒序排列

    // html <html> <div>hello</div> <div>world</div> <a href="&quo ...

  10. 日常工作问题解决:centos/linux系统如何检测端口是否打开

    1.telnet命令 格式: telnet ip 端口号 [root@centos7-127 ~]# telnet 192.168.87.128 22 Trying 192.168.87.128... ...