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. 003-多线程-JUC集合-Set-CopyOnWriteArrayList

    一.概述 它是线程安全的无序的集合,可以将它理解成线程安全的HashSet.有意思的是,CopyOnWriteArraySet和HashSet虽然都继承于共同的父类AbstractSet:但是,Has ...

  2. c++ map multimap操作

    #include <iostream>#include <map>#include <string> using namespace std; int main() ...

  3. 07Flutter ListView基础列表组件、水平列表组件、图标组件

    ListView:     ListView:参数     scrollDirection:Axis.horizontal:水平列表.Axis.vertical垂直列表     padding:内边距 ...

  4. mysql多实例搭建

    一)多实例安装 [root@mysqlmaster01 ~]# mkdir /data/mysql_data2[root@mysqlmaster01 ~]# mkdir /data/mysql_dat ...

  5. Linux 系统中用Systemd 管理系统服务

    Systemd  命令详解: https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-syste ...

  6. Apache Jmeter教程(一) - 入门

    一.下载Jmeter 登录官网Jmeter下载,得到压缩包jmeter-5.0.tgz,下载地址:http://jmeter.apache.org/download_jmeter.cgi. 二.安装 ...

  7. redis 获取方式和安装(windows)

    Windows redis :https://github.com/MSOpenTech/redis/releases Linux redis :https://github.com/phpredis ...

  8. 对于大于等于3的整数n,在区间【n,3/2 * n】中一定存在一个素数

    对于大于3的整数n,在区间[n,3/2 * n]中一定存在一个素数

  9. Feign 接口上传文件

    1)Encoder 配置注入容器 2) public class SpringFormEncoderExtension extends FormEncoder { /** * 使用默认的feign编码 ...

  10. 2017"百度之星"程序设计大赛 - 资格赛 1002 度度熊的王国战略

    全局最小割 Stoer-Wagner (SW算法)优化 优化吃藕了,感谢放宽时限,感谢平板电视 (pb_ds) #include <iostream> #include <cstdi ...