iOS开发- 速学Swift-中文概述
Swift是什么?
Swift是苹果于WWDC 2014公布的编程语言,这里引用The Swift Programming Language的原话:
Swift is a new programming language for iOS and OS X apps that builds on the best of C and Objective-C, without the constraints of C compatibility.
Swift adopts safe programming patterns and adds modern features to make programming easier, more flexible and more fun.
Swift’s clean slate, backed by the mature and much-loved Cocoa and Cocoa Touch frameworks, is an opportunity to imagine how software development works.
Swift is the first industrial-quality systems programming language that is as expressive and enjoyable as a scripting language.
简单的说:
- Swift用来写iOS和OS X程序。(预计也不会支持其他屌丝系统)
- Swift吸取了C和Objective-C的优点,且更加强大易用。
- Swift能够使用现有的Cocoa和Cocoa Touch框架。
- Swift兼具编译语言的高性能(Performance)和脚本语言的交互性(Interactive)。
Swift语言概览
基本概念
注:这一节的代码源自The Swift Programming Language中的A Swift Tour。
Hello, world
类似于脚本语言,以下的代码即是一个完整的Swift程序。
1 |
|
变量与常量
Swift使用var声明变量。let声明常量。
1 |
|
类型推导
Swift支持类型推导(Type Inference),所以上面的代码不需指定类型,假设须要指定类型:
1388941295); background-color: rgb(170, 170, 170); background-position: 50% 0%; background-repeat: repeat no-repeat;">
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
Swift不支持隐式类型转换(Implicitly casting),所以以下的代码须要显式类型转换(Explicitly casting):
1 |
|
字符串格式化
Swift使用\(item)的形式进行字符串格式化:
1388941295); background-color: rgb(170, 170, 170); background-position: 50% 0%; background-repeat: repeat no-repeat;">
1 |
|
数组和字典
Swift使用[]操作符声明数组(array)和字典(dictionary):
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
一般使用初始化器(initializer)语法创建空数组和空字典:
1 |
|
假设类型信息已知。则能够使用[]声明空数组,使用[:]声明空字典。
控制流
概览
Swift的条件语句包括if和switch,循环语句包括for-in、for、while和do-while,循环/推断条件不须要括号,但循环/推断体(body)必需括号:
1 |
|
可空类型
结合if和let,能够方便的处理可空变量(nullable variable)。
对于空值,须要在类型声明后加入?
显式标明该类型可空。
1 |
|
灵活的switch
Swift中的switch支持各种各样的比較操作:
1 |
|
其他循环
for-in除了遍历数组也能够用来遍历字典:
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
while循环和do-while循环:
1 |
|
Swift支持传统的for循环,此外也能够通过结合..(生成一个区间)和for-in实现相同的逻辑。
1 |
|
注意:Swift除了..还有...:..生成前闭后开的区间。而...生成前闭后闭的区间。
函数和闭包
函数
Swift使用funckeyword声明函数:
1 |
|
通过元组(Tuple)返回多个值:
1388941295); background-color: rgb(170, 170, 170); background-position: 50% 0%; background-repeat: repeat no-repeat;">
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
支持带有变长參数的函数:
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
函数也能够嵌套函数:
1 |
|
作为头等对象。函数既能够作为返回值。也能够作为參数传递:
1 |
|
1 |
|
闭包
本质来说,函数是特殊的闭包,Swift中能够利用{}声明匿名闭包:
1388941295); background-color: rgb(170, 170, 170); background-position: 50% 0%; background-repeat: repeat no-repeat;">
1 |
|
当闭包的类型已知时,能够使用以下的简化写法:
1388941295); background-color: rgb(170, 170, 170); background-position: 50% 0%; background-repeat: repeat no-repeat;">
1 |
|
此外还能够通过參数的位置来使用參数,当函数最后一个參数是闭包时,能够使用以下的语法:
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
类和对象
创建和使用类
Swift使用class创建一个类,类能够包括字段和方法:
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
创建Shape类的实例,并调用其字段和方法。
1388941295); background-color: rgb(170, 170, 170); background-position: 50% 0%; background-repeat: repeat no-repeat;">
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
通过init构建对象。既能够使用self显式引用成员字段(name),也能够隐式引用(numberOfSides)。
1388941295); background-color: rgb(170, 170, 170); background-position: 50% 0%; background-repeat: repeat no-repeat;">
1 |
|
使用deinit进行清理工作。
继承和多态
Swift支持继承和多态(override父类方法):
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
注意:假设这里的simpleDescription方法没有被标识为override。则会引发编译错误。
属性
为了简化代码,Swift引入了属性(property)。见以下的perimeter字段:
1 |
|
注意:赋值器(setter)中,接收的值被自己主动命名为newValue。
willSet和didSet
EquilateralTriangle的构造器进行了例如以下操作:
- 为子类型的属性赋值。
- 调用父类型的构造器。
- 改动父类型的属性。
假设不须要计算属性的值。但须要在赋值前后进行一些操作的话,使用willSet和didSet:
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
从而保证triangle和square拥有相等的sideLength。
调用方法
Swift中,函数的參数名称仅仅能在函数内部使用,但方法的參数名称除了在内部使用外还能够在外部使用(第一个參数除外),比如:
1388941295); background-color: rgb(170, 170, 170); background-position: 50% 0%; background-repeat: repeat no-repeat;">
1 |
|
注意Swift支持为方法參数取别名:在上面的代码里。numberOfTimes面向外部。times面向内部。
?的还有一种用途
使用可空值时。?
能够出如今方法、属性或下标前面。
假设?前的值为nil,那么?后面的表达式会被忽略。而原表达式直接返回nil,比如:
1388941295); background-color: rgb(170, 170, 170); background-position: 50% 0%; background-repeat: repeat no-repeat;">
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
当optionalSquare为nil时。sideLength属性调用会被忽略。
枚举和结构
枚举
使用enum创建枚举——注意Swift的枚举能够关联方法:
1388941295); background-color: rgb(170, 170, 170); background-position: 50% 0%; background-repeat: repeat no-repeat;">
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
使用toRaw和fromRaw在原始(raw)数值和枚举值之间进行转换:
1 |
|
注意枚举中的成员值(member value)是实际的值(actual value)。和原始值(raw value)没有必定关联。
一些情况下枚举不存在有意义的原始值。这时能够直接忽略原始值:
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
除了能够关联方法,枚举还支持在其成员上关联值。同一枚举的不同成员能够有不同的关联的值:
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
结构
Swift使用structkeyword创建结构。
结构支持构造器和方法这些类的特性。
结构和类的最大差别在于:结构的实例按值传递(passed by value)。而类的实例按引用传递(passed by reference)。
1388941295); background-color: rgb(170, 170, 170); background-position: 50% 0%; background-repeat: repeat no-repeat;">
1 |
|
协议(protocol)和扩展(extension)
协议
Swift使用protocol定义协议:
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
类型、枚举和结构都能够实现(adopt)协议:
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
扩展
扩展用于在已有的类型上添加新的功能(比方新的方法或属性)。Swift使用extension声明扩展:
1 |
|
泛型(generics)
Swift使用<>来声明泛型函数或泛型类型:
1388941295); background-color: rgb(170, 170, 170); background-position: 50% 0%; background-repeat: repeat no-repeat;">
1 |
|
Swift也支持在类、枚举和结构中使用泛型:
1388941295) !important; background-color: rgb(7, 54, 66) !important; background-position: 0% 0% !important;">1 |
|
有时须要对泛型做一些需求(requirements),比方需求某个泛型类型实现某个接口或继承自某个特定类型、两个泛型类型属于同一个类型等等,Swift通过where描写叙述这些需求:
1 |
|
Swift语言概览就到这里,有兴趣的朋友请进一步阅读The Swift Programming Language。
接下来聊聊个人对Swift的一些感受。
个人感受
注意:以下的感受纯属个人意见。仅供參考。
大杂烩
虽然我接触Swift不足两小时。但非常easy看出Swift吸收了大量其他编程语言中的元素,这些元素包括但不限于:
- 属性(Property)、可空值(Nullable type)语法和泛型(Generic Type)语法源自C#。
- 格式风格与Go相仿(没有句末的分号。推断条件不须要括号)。
- Python风格的当前实例引用语法(使用
self)和列表字典声明语法。 - Haskell风格的区间声明语法(比方
1..3,1...3)。 - 协议和扩展源自Objective-C(自家产品随便用)。
- 枚举类型非常像Java(能够拥有成员或方法)。
class和struct的概念和C#极其类似。
注意这里不是说Swift是抄袭——实际上编程语言能玩的花样基本就这些,况且Swift选的都是在我看来相当不错的特性。
并且。这个大杂烩有一个优点——就是不论什么其他编程语言的开发人员都不会觉得Swift非常陌生——这一点非常重要。
拒绝隐式(Refuse implicity)
Swift去除了一些隐式操作,比方隐式类型转换和隐式方法重载这两个坑,干的美丽。
Swift的应用方向
我觉得Swift主要有以下这两个应用方向:
教育
我指的是编程教育。现有编程语言最大的问题就是交互性奇差,从而导致学习曲线陡峭。相信Swift及其交互性极强的编程环境能够打破这个局面,让很多其他的人——尤其是青少年。学会编程。
这里有必要再次提到Brec Victor的Inventing on Principle。看了这个视频你就会明确一个交互性强的编程环境能够带来什么。
应用开发
现有的iOS和OS X应用开发均使用Objective-C,而Objective-C是一门及其繁琐(verbose)且学习曲线比較陡峭的语言,假设Swift能够提供一个同现有Obj-C框架的简易互操作接口,我相信会有大量的程序猿转投Swift;与此同一时候,Swift简易的语法也会带来相当数量的其他平台开发人员。
总之,上一次某家大公司大张旗鼓的推出一门编程语言及其编程平台还是在2000年(微软推出C#),将近15年之后。苹果推出Swift——作为开发人员。我非常高兴能够见证一门编程语言的诞生。
iOS开发- 速学Swift-中文概述的更多相关文章
- 快看Sample代码,速学Swift语言(2)-基础介绍 快看Sample代码,速学Swift语言(1)-语法速览
快看Sample代码,速学Swift语言(2)-基础介绍 Swift语言是一个新的编程语言,用于iOS, macOS, watchOS, 和 tvOS的开发,不过Swift很多部分内容,我们可以从C或 ...
- 突破,Objective-C开发速学手册
<突破,Objective-C开发速学手册> 基本信息 作者: 傅志辉 出版社:电子工业出版社 ISBN:9787121207426 上架时间:2013-7-12 出版日期:2013 年8 ...
- 快看Sample代码,速学Swift语言(1)-语法速览
Swift是苹果推出的一个比较新的语言,它除了借鉴语言如C#.Java等内容外,好像还采用了很多JavaScript脚本里面的一些脚本语法,用起来感觉非常棒,作为一个使用C#多年的技术控,对这种比较超 ...
- iOS开发——技术精华Swift篇&Swift 2.0和Objective-C2.0混编之第三方框架的使用
swift 语言是苹果公司在2014年的WWDC大会上发布的全新的编程语言.Swift语言继承了C语言以及Objective-C的特性,且克服了C语言的兼容性问题.Swift语言采用安全编程模式,且引 ...
- iOS开发中提交带有中文或特殊字符串的参数
iOS开发中,与后台进行数据交换是一个很常见的场景. 在web开发中,对于我们提交的地址,浏览器会负责进行decode,但是在ios中,必须要自己手动来实现.否则我们拼接出的网址在包括中文.特殊字符串 ...
- iOS开发——新特性Swift篇&Swift 2.0 异常处理
Swift 2.0 异常处理 WWDC 2015 宣布了新的 Swift 2.0. 这次重大更新给 Swift 提供了新的异常处理方法.这篇文章会主要围绕这个方面进行讨论. 如何建造异常类型? 在 i ...
- 快看Sample代码,速学Swift语言(2)-基础介绍
Swift语言是一个新的编程语言,用于iOS, macOS, watchOS, 和 tvOS的开发,不过Swift很多部分内容,我们可以从C或者Objective-C的开发经验获得一种熟悉感.Swif ...
- iOS开发零基础--Swift教程 可选类型
可选类型的介绍 注意: 可选类型时swift中较难理解的一个知识点 暂时先了解,多利用Xcode的提示来使用 随着学习的深入,慢慢理解其中的原理和好处 概念: 在OC开发中,如果一个变量暂停不使用,可 ...
- iOS开发——网络编程Swift篇&Alamofire详解
Alamofire详解 预览图 Swift Alamofire 简介 Alamofire是 Swift 语言的 HTTP 网络开发工具包,相当于Swift实现AFNetworking版本. 当然,AF ...
随机推荐
- [算法]滴滴笔试题——求最大子串和(O(n)复杂度)
扫描法.一次扫描数组即可得出答案,复杂度O(n).这种方法用文字描述不容易说清楚,下面用每一步运算的图示来表达.伪代码如下: maxsofar=end=; ,n) end=max(end+x[i],) ...
- C#调用系统API
API简介 1) C#中的简单数据类型与API中的数据类型对应关系 2) 如何在调用API时传递复杂参数:封装类.结构和联合 3) 如何调用API 4) 如何确保成功调用API
- [转]谈谈Linux下动态库查找路径的问题
http://blog.chinaunix.net/uid-23069658-id-4028681.html 学习到了一个阶段之后,就需要不断的总结.沉淀.清零,然后才能继续“上路”.回想起自己当年刚 ...
- log4j配置文件
log4j.rootLogger=INFO,CONSOLElog4j.addivity.org.apache=truelog4j.appender.stdout=org.apache.log4j.Co ...
- 采用std::thread 替换 openmp
内容转换的,具体详见博客:https://cloud.tencent.com/developer/article/1094617 及对应的code:https://github.com/cpuimag ...
- 使用Audio API设计绚丽的HTML5音乐播放器
HTML5 有两个很炫的元素,就是Audio和 Video,可以用他们在页面上创建音频播放器和视频播放器,制作一些效果很不错的应用. 无论是视屏还是音频,都是一个容器文件,包含了一些音频轨道,视频轨道 ...
- git练习
git commit 提交记录 git branch <branch_name> 建立名为branch_name的分支 git checkout <name>:git comm ...
- Mysql 知识点总结
1.创建数据库 mysqladmin 下面是一个简单的例子,创建名为 yiibai_tutorials1 的数据库. D:\software\mysql--winx64\bin> mysq ...
- string整理
/* scanf是c语言的,而string是c++的类,所以不能使用scanf直接读入 */ #include<cstdio> #include<string>//注意支持库 ...
- python练习题集合-2
author:headsen chen date:2018-06-01 15:39:26 习题17,文件的更多操作 [root@localhost py]# echo > cc.txt [ro ...