当前位置:网站首页>OC和Swift语言的区别
OC和Swift语言的区别
2022-08-02 02:23:00 【~废弃回忆 �༄】
一.编程范式
Swift可以面向协议编程,函数式编程,面向对象编程.
OC已面向对象编程为主,当然你可以引入类似ReactiveCocoa的类库来进行函数式编程.
二.类型安全
Swift是一门类型安全的语言.鼓励程序员在代码中清楚明确值得类型.如果在代码中使用一个字符串String,那么你不能错误地传递一个整型int给它.因为Swift是类型安全的,他会在代码编译的时候做类型检查,并且把所有不匹配的类型作为一个错误标记出来.
而OC则不然,你声明一个NSString变量,仍然可以传一个NSNumber给它,尽管编译器会抱怨,但是你仍然可以作为NSNumber来使用它.
三.值类型
在Swift中,典型的有Struct,enum以及tuple都是值类型.而平时使用的int ,double,float,string,Array,Dictionary,Set其实都是使用结构体实现的,也是值类型.
OC中,NSNumber,NSString以及集合类对象都是指针类型.
四.枚举增强
Swift的枚举可以使用整型,浮点型,字符串,还能拥有属性和方法,甚至支持泛型,协议,扩展等等.
OC中的枚举鸡肋则很多.
五.泛型
Swift中支持泛型,也支持泛型的类型约束等特性.
苹果推出了Swift2.0版本,为了让开发者从OC更好的过渡到Swift上,苹果也为OC带来了Generics泛型支持,不过OC的泛型也仅停留在编译器警告阶段.
六.协议和扩展
Swift对协议的支持更加丰富,配合扩展(extension),泛型,关联类型等可以实现面向协议编程,从而大大提高代码的灵活性.同时,Swift中的protocol还可以用于值类型,如结构体和枚举.
OC的协议缺乏强约束,提供的optiional特性往往成为很多问题的来源,而如果放弃optional又会让实现代价过大.
七.函数赫尔闭包
Swift函数一等公民,可以直接定义函数类型变量,可以作为其他函数参数传递,可以作为函数返回值返回.
OC里面函数仍然是次等公民,需要selector封装或者使用block才能模拟Swift中类似的效果.
边栏推荐
- CodeTon Round 2 D. Magical Array 规律
- AntPathMatcher uses
- 53. 最小的k个数
- Garbage Collector CMS and G1
- AI target segmentation capability for fast video cutout without green screen
- Oracle数据类型介绍
- How to adjust the cross cursor too small, CAD dream drawing calculation skills
- A good book for newcomers to the workplace
- Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
- 790. 数的三次方根
猜你喜欢

Garbage Collector CMS and G1

个人博客系统项目测试

网络层解析——IP协议、地址管理、路由选择

2022-07-30 mysql8 executes slow SQL-Q17 analysis

nacos启动报错,已配置数据库,单机启动

数值积分方法:欧拉积分、中点积分和龙格-库塔法积分

Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products

Electronic Manufacturing Warehouse Barcode Management System Solution

2022河南青训联赛第(三)场

Analysis of the status quo of digital transformation of manufacturing enterprises
随机推荐
个人博客系统项目测试
Remember a pit for gorm initialization
Outsourcing worked for three years, it was abolished...
The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
极大似然估计
From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
LeetCode brushing diary: 33. Search and rotate sorted array
CASE2023
libcurl访问url保存为文件的简单示例
使用docker安装mysql
Golang分布式应用之Redis
Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
BI-SQL丨WHILE
优炫数据库导库导错了能恢复吗?
永磁同步电机36问(三)——SVPWM代码实现
ros多客户端请求服务
记一次gorm事务及调试解决mysql死锁
接口测试神器Apifox究竟有多香?
工程师如何对待开源
Win Go development kit installation configuration, GoLand configuration