The Secret Life of Types in Swift

At first glance, Swift looked different as a language compared to Objective-C because of the modern code syntax. Secondly, the strict and strong type system is also different.
This talk takes an under-the-hood deep dive into the Swift type system’s structure and gives tips how to use it in a proper way.
Introduction
I’m Manu, and I will talk to you about data types today. Swift is pretty simple compared to other languages, and Swift is pretty clear in its architecture of the typing system.
It’s All About Types
There are two things to know first. First, Swift says there shall not be root - $noRoot. And we will never have nil.

There’s no specific root type in Swift. For example, in other languages like Java, or C#, or static type systems, they have a root type. Swift does not have a root type, and empty values are not allowed.
In Java, an Integer is just a number. The inheritance line is not long, but we do have two stages of inheritance over the type itself, with interfaces, which conforms to a protocol. Objective-C is similar, as it also conforms to a protocol.

In Swift, Int is a type of itself, and it defines its data types as structures, not classes.
Dive into Types
In Swift, everything is a structure with no inheritance. The reason this is done is so we can have super loose coupling among each type, and can be extended throughout the whole type system. This allows for clean architecture.
Named Types

Named types are types that have names. It’s classes, structures, Enums, and protocols; essentially everything you are giving a name when you are using it.
Compound Types

A compound type is a type with no name. The two types of compound types are tuples and functions.
Tuples
This greatTuple variable has exactly the type Int, Int. This is a real type, it just looks like a bit different.
You can attach, or assign a different Int, Int tuple. To do so, leave the parameters, and use the numbers themselves. You can enter the values with the .0, and .1, and so on. Or, you can use the parameter names again.
Let’s continue with tuples, and where they are really meaningful.
Suppose we want to return more than one value from functions. This is done by just defining the types in the return as a tuple.
Tuple types are unnamed but you can give them a name. With the typealias, you can do exactly this.
Here, we are defining a tuple of just two Ints. There is a variable named Point.
Function Types
Like tuples, functions types are also common.
If I want to return nothing, you can get by with writing nothing. The thing exactly after the name of the function, until the first bracket, is the function type.
https://academy.realm.io/posts/altconf-2017-manu-rink-secret-life-of-types-in-swift/?w=1
The Secret Life of Types in Swift的更多相关文章
- iOS: 学习笔记, 值与引用类型(译自: https://developer.apple.com/swift/blog/ Aug 15, 2014 Value and Reference Types)
值和引用类型 Value and Reference Types 在Swift中,有两种数据类型. 一是"值类型"(value type), 它是每一个实例都保存有各自的数据,通常 ...
- swift语言点评十-Value and Reference Types
结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...
- swift学习笔记1——基础部分
之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...
- 理解 OpenStack Swift (1):OpenStack + 三节点Swift 集群+ HAProxy + UCARP 安装和配置
本系列文章着重学习和研究OpenStack Swift,包括环境搭建.原理.架构.监控和性能等. (1)OpenStack + 三节点Swift 集群+ HAProxy + UCARP 安装和配置 ( ...
- Swift互用性:与 Cocoa 数据类型共舞(Swift 2.0版)-b
本节内容包括: 字符串(Strings) 数值(Numbers) 集合类(Collection Classes) 错误(Errors) Foundation数据类型(Foundation Data T ...
- Swift: Associated Types--为什么协议使用关联类型而不是泛型
关联类型的形式为类型的引用进而进行约束提供了条件: 同时能够简化语法形式. Swift: Associated Types http://www.russbishop.net/swift-associ ...
- swift,NSUserDefaults的swift化封装
NSUserDefaultshtml, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror- ...
- swift内存管理:值类型与引用类型
Use struct to create a structure. Structures support many of the same behaviors as classes, includin ...
- 行走于Swift的世界中(转)
从Swift正式公布到现在,我基本一直在关注和摸索Swift.对于一门新语言来说,开荒阶段的探索自然是激动人心的,但是很多时候,资料的缺失和细节的隐藏也让人着实苦恼.最近几天的感受是,Swift 并不 ...
随机推荐
- 用ADB 抓取和存储APP日志方法
前置条件:电脑已经安装好adb (一) 进入adb目录下连接手机,检测出手机 CD 到SDK的platform-tools (二)adb logcat-c 清除日志 (三)adb logcat ...
- | C语言I作业01
C语言I作业01 标签:18软件 李煦亮 1.1 你对软件工程专业了解是怎样? 对软件工程的了解是从人工智能频繁地出现在各大新闻,新闻报道了许多高校针对人工智能开设了相关课程或者专业,软件工程是开设的 ...
- 一个刚入行的BIOS工程师的自我简介
现在是北京时间2019年11月28日,大学毕业已经工作四个多月.说来也是奇怪,大学里面明明主修机械电子工程,几乎是纯机械方向,毕业之后的工作却与主修的课程毫无关系.因为对机械这一行业毫无兴趣,大学里面 ...
- 【Python爬虫案例学习】Python爬取天涯论坛评论
用到的包有requests - BeautSoup 我爬的是天涯论坛的财经论坛:'http://bbs.tianya.cn/list.jsp?item=develop' 它里面的其中的一个帖子的URL ...
- jboss/wildfly安全域的密码加密和解密
加密: java_path=$(source /opt/wildfly/bin/.Beta1.jar:/opt/wildfly/modules/system/layers/base/org/jboss ...
- Win10应用商城删除后部分应用出错的解决方案
出错图示 解决方案 查找完整包名:Get-AppxPackage -allusers | Select Name, PackageFullName Add-appxpackage -register ...
- python环境安装及其就业状况
一,下载及安装 1.进入官网下载 2.安装 二,就业前景 1.Python就业行情和前景分析之一 岗位数量 2..Python就业行情和前景分析之一 学历要求 3.工资状况
- 这两个小技巧,让我的SQL语句不仅躲了坑,还提升了1000 倍
原文: https://cloud.tencent.com/developer/article/1465618 本次来讲解与 SQL 查询有关的两个小知识点,掌握这些知识点,能够让你避免踩坑以及提高查 ...
- BZOJ1040: [ZJOI2008]骑士(奇环树,DP)
题目: 1040: [ZJOI2008]骑士 解析: 假设骑士\(u\)讨厌骑士\(v\),我们在\(u\),\(v\)之间连一条边,这样我们就得到了一个奇环树(奇环森林),既然是一颗奇环树,我们就先 ...
- ios app UI自动化测试用到的命令
ios测试的app测试包,真机设备需要开发者证书并且将测试机的udid加入到pp文件文件,configruation 要求为debug模式的ipa包, 1.苹果手机的UDID, a.通过 xcode- ...