原文链接
Optional Chaining
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html#//apple_ref/doc/uid/TP40014097-CH21-ID245
Deinitialization
Optional Chaining
1、if the optional isnil,the property,method,or subscript call returnsnil. Multiple queries can be chained together,and the entire chain fails gracefully if any link in the chain isnil.
如果任何选择类型是空的,那么属性、方法、以及下标就会返回nil,许多问询能被组成链条,只要任何链接是空的,整个链条就会优雅的失败。
2、Optional chaining in Swift is similar to messagingnil in Objective-C,but in a way that works for any type,and that can be checked for success or failure.
在swift中选择链条类似在oc中给nil发消息,但是这个是在任何类型都工作,而且能被检查失败还是成功。
3、Because the attempt to accessnumberOfRooms has the potential to fail,the optional chaining attempt returns a value of typeInt?,or “optionalInt”
因为接近numberOfRooms有失败的潜力,所以这个选择链尝试返回一个整数值或者选择性的整数。
4、Whenresidence isnil,as in the example above,this optionalInt will also benil,to reflect the fact that it was not possible to accessnumberOfRooms.
当residence时空的时候,就像以上的例子,选择的int将会是空的,反映到事实上,就是不可能接近的属性numberOfRooms
5、classResidence {
- varrooms = [Room]()
- varnumberOfRooms:Int {
- returnrooms.count
- }
- subscript(i:Int) -> Room {
- get {
- returnrooms[i]
- }
- set {
- rooms[i] =newValue
- }
- }
- funcprintNumberOfRooms() {
- print("The number of rooms is\(numberOfRooms)")
- }
- varaddress:Address?
- }
- As a shortcut to accessing itsrooms array,this version ofResidence provides a read-write subscript that provides access to the room at the requested index in therooms array.
作为一个接近rooms数组的的快捷键,Residence这个版本的提供了一个只读的下标,通过这个index获取数组里面的东西。
6、As demonstrated inOptional Chaining as an Alternative to Forced Unwrapping,you can use optional chaining to access a property on an optional value,and to check if that property access is successful.
正如在上面声明的那样,你能用选择链来接近属性,来检查属性的途径是否成功。
未看完
Deinitialization
1、Adeinitializer is called immediately before a class instance is deallocated
一个实例被释放,这个反初始器就会马上被调动。
2、You write deinitializers with thedeinit keyword,similar to how initializers are written with theinit keyword. Deinitializers are only available on class types.
你用deinit关键字声明一个去初始器,类似于你用init关键字声明一个初始器一样,不过,反初始器只在类中唯一使用。
3、However,when you are working with your own resources,you might need to perform some additional cleanup yourself. For example,if you create a custom class to open a file and write some data to it,you might need to close the file before the class instance is deallocated.
然而,当你处理你的自己的资源时,你需要自己完成一些额外的清理,例如,如果你创建一个定制的类来打开一个文件或者给这个文件写数据,你需要在在类释放前关闭文件。
4、Class definitions can have at most one deinitializer per class. The deinitializer does not take any parameters and is written without parentheses:
每个类里面类定义里面最多有一个反初始器,反初始器不能采用任何参数,不能写在圆括号外。
5、Deinitializers are called automatically,just before instance deallocation takes place.
去初始化定义是自动的,在实例变量去初始化之前。
6、the superclass deinitializer is called automatically at the end of a subclass deinitializer implementation
7、Superclass deinitializers are always called,even if a subclass does not provide its own deinitializer.
父类去初始化总是被调用,即使一个子类没有提供它自己的去初始器。
8、Because an instance is not deallocated until after its deinitializer is called,a deinitializer can access all properties of the instance it is called on and can modify its behavior based on those properties (such as looking up the name of a file that needs to be closed).
因为一个实例直到它的去初始器被调用,它才会被释放,一个去去初使器能接近一个实例的所有属性,而且能修改那些基于这些属性的行为(例如,寻找需要被关闭的文件的名字)
9、manages a made-up currency
管理一个虚构的货币。
to here ----->Deinitializers in Action
原文链接:https://www.f2er.com/swift/321738.html