swift学习笔记0

开始学swift,随便记录一下。(英文部分摘自官方文档)

1 optionals 可选

You use optionals in situations where a value may be absent. An optional represents two possibilities: Either there is a value, and you can unwrap the optional to access that value, or there isn’t a value at all.

如果一个值可能是空的,就可以使用optionals。optional 表示有两种可能性:要么存在一个值(你可以获取这个值),要么值根本不存在。
看个官方的例子:

Here’s an example of how optionals can be used to cope with the absence of a value. Swift’s Int type has an initializer which tries to convert a String value into an Int value. However, not every string can be converted into an integer. The string “123” can be converted into the numeric value 123, but the string “hello, world” does not have an obvious numeric value to convert to.

The example below uses the initializer to try to convert a String into an Int:

let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
// convertedNumber is inferred to be of type "Int?", or "optional Int"

Because the initializer might fail, it returns an optional Int, rather than an Int. An optional Int is written as Int?, not Int. The question mark indicates that the value it contains is optional, meaning that it might contain some Int value, or it might contain no value at all. (It can’t contain anything else, such as a Bool value or a String value. It’s either an Int, or it’s nothing at all.)

在swift中,Int类型有一个初始方法,把string类型的值转换为Int值。但是,并不是每个string都能转换为Int。“123”这个字符串可以转换为数字值123,但是字符串“hello world”就不能转换为数字值。

let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
// convertedNumber 指向的类型是 "Int?", 或者说 "optional Int" ,可选的int

因为初始方法可能会失败,所以它返回的是一个可选的int,而不是int。可选的int(optional Int)写作:Int?. 后面的问号表示是可选的,即它或许有一个int值,或许根本没值。(它也不能保存其他如bool或string的值,它要么有一个int值,要么没值)

Implicitly Unwrapped Optionals 隐式解析可选

Sometimes it is clear from a program’s structure that an optional will always have a value, after that value is first set. In these cases, it is useful to remove the need to check and unwrap the optional’s value every time it is accessed, because it can be safely assumed to have a value all of the time.

有时候,从程序中可以确定一个optional 在第一次设置之后 永远都有一个值,那么就没必要每次获取值的时候都去检查与解析(check and unwrap),而是可以假设它永远都有一个值。

These kinds of optionals are defined as implicitly unwrapped optionals. You write an implicitly unwrapped optional by placing an exclamation mark (String!) rather than a question mark (String?) after the type that you want to make optional.

那么这种类型的optionals就定义为implicitly unwrapped optionals(隐式解析可选)。隐式解析可选的写法:在类型的后面加个感叹号!而不是问号?.

The following example shows the difference in behavior between an optional string and an implicitly unwrapped optional string when accessing their wrapped value as an explicit String:

看两个例子:

let possibleString: String? = "An optional string."
let forcedString: String = possibleString! // 感叹号要写上

let assumedString: String! = "An implicitly unwrapped optional string."
let implicitString: String = assumedString // 不需要感叹号

第一个例子中,possibleString是一个可选的string,并且被赋了一个值,forcedString 是一个string类型,要把possibleString的值赋给forcedString 就必须对possibleString进行隐式解析,即在后面加个感叹号。如果不加,会报错。
第二个例子中,assumedString已经是一个解析过的string类型了,可以直接把值赋给implicitString。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值