swift语言点评一
一、变量定义
1、常量与变量
Use let
to make a constant and var
to make a variable.
2、类型与推测
However, you don’t always have to write the type explicitly. Providing a value when you create a constant or variable lets the compiler infer its type.
3、类型转换与字符串
Write the value in parentheses, and write a backslash (\
) before the parentheses.
let appleSummary = "I have \(apples) apples."
4、集合类型:
let emptyArray = [String]()
let emptyDictionary = [String: Float]()
总结:(1)类型写在变量后,(2)类型可推断,(3)便捷的字符串化操作。
二、控制流
1、控制流综述
Use if
and switch
to make conditionals, and use for
-in
, while
, and repeat
-while
to make loops.
2、optional value ?
An optional value either contains a value or contains nil
to indicate that a value is missing.
3、default value ??
4、if let 判断是否为非空
You can use if
and let
together to work with values that might be missing.
5、if 后只能跟bool
6、swich 可以使用任意值
7、
let x where x.hasSuffix("pepper")
将switch值赋给x,如果复合范式,最终会赋给x
where vegetable.hasSuffix可以用本体;
8、for
-in 使用了类型推断技术
for (kind, numbers) in interestingNumbers 可以用于字典;
总结:
if的特别性、?、??、switch支持任意类型。
三、函数与闭包
1、函数形式
func greet(person: String, day: String) -> String{}
简化表示:()->(){}
2、no argument label
参量无名调用;向c++靠近;
3、多输出函数
func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int)
4、高阶函数
5、闭包及其简化。
总结:
Functions are a first-class type
三、类
1、类的初始化函数化
var triangle = EquilateralTriangle(sideLength: 3.1, name: "a triangle")
2、 属性:willSet
and didSet getter and a setter
3、 If the value before the ?
is nil
, everything after the ?
is ignored and the value of the whole expression is nil
.
总结:类的使用函数化,属性的访问控制便捷化。
四、枚举与结构
1、类与结构的区别
One of the most important differences between structures and classes is that structures are always copied when they are passed around in your code, but classes are passed by reference.
2、枚举Use the init?(rawValue:)
initializer
Use the init?(rawValue:)
initializer to make an instance of an enumeration from a raw value. It returns either the enumeration case matching the raw value or nil
if there is no matching Rank
if let convertedRank = Rank(rawValue: 3)
3、枚举值的直接引用
let hearts = Suit.hearts
4、结构型枚举
enum ServerResponse {
case result(String, String)
case failure(String)
}
let success = ServerResponse.result("6:00 am", "8:09 pm")
let failure = ServerResponse.failure("Out of cheese.")
五、协议与扩展
与oc语言无区别;
扩展可以针对任何类型。
六、异常捕获
Another way to handle errors is to use try?
to convert the result to an optional. If the function throws an error, the specific error is discarded and the result is nil
. Otherwise, the result is an optional containing the value that the function returned.
let printerSuccess = try? send(job: 1884, toPrinter: "Mergenthaler")
let printerFailure = try? send(job: 1885, toPrinter: "Never Has Toner")
七、defer
Use defer
to write a block of code that is executed after all other code in the function, just before the function returns.
八、范型
<>
swift语言点评一的更多相关文章
- swift语言点评四-Closure
总结:整个Closure的作用在于简化语言表述形式. 一.闭包的简化 Closure expression syntax has the following general form: { () -& ...
- swift语言点评二
一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...
- swift语言点评十九-类型转化与检查
1.oc比较: -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例 -(BOOL) isMemberOfClass: classObj 判断是否是这个 ...
- swift语言点评十八-异常与错误
1.错误类型与枚举的结合 enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNee ...
- swift语言点评十七-Designated Initializers and Convenience Initializers
Swift defines two kinds of initializers for class types to help ensure all stored properties receive ...
- swift语言点评十四-继承
Overriding A subclass can provide its own custom implementation of an instance method, type method, ...
- swift语言点评十-Value and Reference Types
结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...
- swift语言点评八-枚举
总结:swift中的枚举可以看作变量可以作为case匹配参数的类 Enumerations 枚举的作用:状态列举与匹配 枚举值与类型 If a value (known as a “raw” valu ...
- swift语言点评五-Function
一.函数类型 Every function in Swift has a type, consisting of the function’s parameter types and return t ...
随机推荐
- 高德SDK获取到的坐标转换为GPS真实坐标方法,Java版
发现高德SDK不提供高德的坐标转GPS坐标(GCJ_02转WGS_84),下面是一份Java版的 /**************************** 文件名:GCJ2WGS.java 创建时间 ...
- socket代码(简单)
SERVER: #!/usr/bin/python # -*- coding: utf-8 -*- import socket import time s = socket.socket(socket ...
- CorelDRAW设计制作超漂亮的3D立体字效果实例教程
第一.打上招聘两个字,选择一个喜欢的字体,如下. 第二.把字体转曲,等待下一步编辑曲线做好准备,如下. 第三.选择形状工具,编辑字体形状,根据自己需要可任意编辑,如下. 第四.适当画一些装饰素材,比如 ...
- v-model指令后面跟的参数(number、lazy、debounce)
1. number 想将用户的输入自动转换为Number类型(如果原值的转换结果为NaN, 则返回原值) 2. lazy 在默认情况下, v-model在input事件中同步输入框的值和数据, 我们可 ...
- 小学生绞尽脑汁也学不会的python(反射)
小学生绞尽脑汁也学不会的python(反射) 1. issubclass, type, isinstance issubclass 判断xxxx类是否是xxxx类的子类 type 给出xxx的数据类型 ...
- js 如何判断浏览器是否禁用cookie
语法:navigator.cookieEnabled: 如果浏览器启用了cookie,该属性值为true.如果禁用了cookie,则值为false. navigator: JavaScript中的一个 ...
- 读取bin文件,并且按结构体赋值打印
目标:读取一个bin文件,并且将bin文件中的数据,按字节对齐赋值给结构体,并且打印出结构体的内容 目前思路是简单的先将bin文件数据一次性读到一个数组中,再将数组强制转换为结构体 ] FILE *f ...
- webpack实战---安装操作
什么是webpack? 他有什么优点? 首先对于很多刚接触webpack人来说,肯定会问webpack是什么?它有什么优点?我们为什么要使用它? Webpack是前端一个工具,可以让各 ...
- JavaScript中的“闭包”
什么是JavaScript中的“闭包”?举一个例子. 闭包是一个内部函数,它可以访问外部(封闭)函数的作用域链中的变量.闭包可以访问三个范围内的变量;具体来说: (1)变量在其自己的范围内, (2)封 ...
- django开发微信公众平台遇到的问题记录
在pythonanywhere.com上使用django开发微信公众平台应用,结果用户发送的信息,微信服务器一次也没有成功转发到pythonanywhere上来,但是用接口测试工具调试却发现是正常的, ...