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

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的更多相关文章

  1. iOS: 学习笔记, 值与引用类型(译自: https://developer.apple.com/swift/blog/ Aug 15, 2014 Value and Reference Types)

    值和引用类型 Value and Reference Types 在Swift中,有两种数据类型. 一是"值类型"(value type), 它是每一个实例都保存有各自的数据,通常 ...

  2. swift语言点评十-Value and Reference Types

    结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...

  3. swift学习笔记1——基础部分

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  4. 理解 OpenStack Swift (1):OpenStack + 三节点Swift 集群+ HAProxy + UCARP 安装和配置

    本系列文章着重学习和研究OpenStack Swift,包括环境搭建.原理.架构.监控和性能等. (1)OpenStack + 三节点Swift 集群+ HAProxy + UCARP 安装和配置 ( ...

  5. Swift互用性:与 Cocoa 数据类型共舞(Swift 2.0版)-b

    本节内容包括: 字符串(Strings) 数值(Numbers) 集合类(Collection Classes) 错误(Errors) Foundation数据类型(Foundation Data T ...

  6. Swift: Associated Types--为什么协议使用关联类型而不是泛型

    关联类型的形式为类型的引用进而进行约束提供了条件: 同时能够简化语法形式. Swift: Associated Types http://www.russbishop.net/swift-associ ...

  7. swift,NSUserDefaults的swift化封装

    NSUserDefaultshtml, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror- ...

  8. swift内存管理:值类型与引用类型

    Use struct to create a structure. Structures support many of the same behaviors as classes, includin ...

  9. 行走于Swift的世界中(转)

    从Swift正式公布到现在,我基本一直在关注和摸索Swift.对于一门新语言来说,开荒阶段的探索自然是激动人心的,但是很多时候,资料的缺失和细节的隐藏也让人着实苦恼.最近几天的感受是,Swift 并不 ...

随机推荐

  1. [转帖]【mount】Linux根目录空间不足

    [mount]Linux根目录空间不足 2019.04.15 21:30:47字数 1094阅读 107 一.问题背景 一台数据库服务器,突然监控告警,报根目录空间不足(no space left o ...

  2. Lua table concat

    [1]table concat 简介 使用方式: table.concat(table, sep, start, end) 作用简介: concat是concatenate(连锁.连接)的缩写. ta ...

  3. C#RSA对接JAVA中RSA方式

    C#中通过FromXmlString属性加载的是XML形式,而JAVA中用到的是解析后的PEM格式的字符串,总之读取证书中信息无非是转换方式问题 /// <summary> /// c# ...

  4. [shell] while read line 与for循环的区别

    [shell] while read line 与for循环的区别 while read line 与for循环的区别---转载整理 while read line 是一次性将文件信息读入并赋值给变量 ...

  5. 推送一个docker 使用阿里docker hub

    阿里docker hub 地址 打开容器镜像服务页https://cr.console.aliyun.com/cn-qingdao/namespaces 这个是我的私有库 配置加速 我这边用的也是阿里 ...

  6. 一张图看懂SharpCamera

    通过下面的图片,可以瞬间看懂整个类库的脉络.

  7. Spring Cloud的常规组件和简单实战(一)

    最近一段时间在学习Spring Cloud,从Eureka到Hystrix,常用的配置和方法都有涉及一些,以此笔记来记录一下学习到的东西,也分享一下.内容以实战为起点,主要以介绍常规用法为主,最后也会 ...

  8. jQuery事件(四)

    一.基本事件函数下面事件函数中参数相关说明:eventType:事件类型,字符串'click' 'submit'多个事件类型可以通过用空格隔开[一次性绑定'click submit']eventDat ...

  9. react 爬坑记录

    1.父子组件优化其一发生render条件:数据改变(state或者props改变),有时子组件会过多render.这时可在子组件里面的生命周期钩子里执行 shouldComponentUpdate(n ...

  10. vector中的push_back函数的意思是什么

    push_back   就是在vector的末尾插入一个元素, vector 中的erase()函数,从指定容器删除指定位置的元素或者某段范围内的元素,删除之后,返回值也是一个迭代器,指向最后一个删除 ...