本文首发于个人我的博客html
这篇文章分享一下Swift中的属性github
关于这个规定,咱们在Swift之结构体 一文中已经说过了,这里稍微提一下,好比下面代码,x和y都是存储属性,当初始化的时候,若是没值,编译器会直接报错。编程
struct Point{
var x: Int
var y: Int
}
var p1 = Point(x: 10, y: 20)
var p2 = Point(y: 20) //报错 Missing argument for parameter 'x' in call
var p3 = Point(x: 10) //报错 Missing argument for parameter 'y' in call
var p4 = Point() //报错 Missing argument for parameter 'x' in call
复制代码
以下面结构体Circle
包括了存储属性radius
和计算属性diameter
swift
truct Circle {
// 存储属性
var radius: Double
// 计算属性
var diameter: Double {
set {
radius = newValue / 2
}
get {
return radius * 2
}
}
}
var circle = Circle(radius: 5)
print(circle.radius) // 5.0
print(circle.diameter) // 10.0
circle.diameter = 12
print(circle.radius) // 6.0
print(circle.diameter) // 12.0
复制代码
struct Circle {
var radius: Double
var diameter: Double {
get {
radius * 2
}
}
}
复制代码
只读计算属性能够简写,例如上面的代码能够以下表示安全
struct Circle {
var radius: Double
var diameter: Double { radius * 2 }
}
复制代码
关于不占用实例的内存,能够以下代码证实bash
struct Circle {
// 存储属性
var radius: Double
// 计算属性
var diameter: Double {
set {
radius = newValue / 2
}
get {
return radius * 2
}
}
}
print("Double占用字节", MemoryLayout<Double>.stride)
print("Circle占用字节",MemoryLayout<Circle>.stride) // 8
复制代码
输出为app
Double占用字节 8
Circle占用字节 8
复制代码
也就是说Circle
占用的仅仅是其存储属性radius
所占用的内存。和计算属性无关的,读者也能够多写几个计算属性,自行验证。ide
代码以下函数
struct Circle {
// 存储属性
var radius: Int
// 计算属性
var diameter: Int {
set {
radius = newValue / 2
}
get {
return radius * 2
}
}
}
var circle = Circle(radius: 9)
circle.radius = 5
circle.diameter = 8 //这里打断点
复制代码
5存储到了全局变量0x3e96(%rip)
,全局变量只有circle
,因此也就是说存储属性的值,被直接放在告终构体的内存中。
8赋值给寄存器%r8d
,又给寄存器%edi,做为参数调用函数0x100001ae0
的时候传入,而函数0x100001ae0
就是testSwift.Circle.diameter.setter
这就是存储属性和计算属性的区别
在Swift枚举一文中,咱们说过枚举原始值是不占用内存的。
enum Season: Int{
case test1 = 1, test2, test3, test4
}
var s = Season.test2
print(s.rawValue) //输出2
复制代码
上面代码输出为2。 这些原始值 test1 = 1, test2, test3, test4,系统内部彻底能够写成只读计算属性
enum Season: Int{
case test1 = 1, test2, test3, test4
var rawValue : Int {
switch self {
case .test1:
return 11
case .test2:
return 12
case .test3:
return 13
case .test4:
return 14
}
}
}
var s = Season.test2
print(s.rawValue) //输出12
复制代码
上面代码输出为12,这样就完成了获取枚举的原始值的时候,直接获取的是只读计算属性。光看结果是不够使人信服的,那就看汇编
enum Season: Int{
case test1 = 1, test2, test3, test4
}
var s = Season.test2
print(s.rawValue) //这里打断点
复制代码
上述代码在最后一行打断点
能够看到,确实调用了rawValue.getter
。
因此汇编才是看出本质的神器。
eg:有类 Car和类Person ,car做为Person的延迟存储属性,那么当使用car的是,才会调用car的初始化方法。
class Car {
init() {
print("Car init!")
}
func run() {
print("Car is running!")
}
}
class Person {
lazy var car = Car()
init() {
print("Person init!")
}
func goOut() {
car.run()
}
}
let p = Person()
print("--------")
p.goOut()
复制代码
输出为
Person init!
--------
Car init!
Car is running!
复制代码
struct Circle {
var radius: Double {
willSet {
print("willSet", newValue)
}
didSet {
print("didSet", oldValue, radius)
}
}
init() {
self.radius = 1.0
print("Circle init!")
}
}
var circle = Circle()// 输出 Circle init!
circle.radius = 10.5
// 输出 willSet 10.5
// didSet 1.0 10.5
print(circle.radius) //输出 10.5
复制代码
eg:
var num: Int {
get {
return 10
}
set {
print("setNum", newValue)
}
}
num = 11 // setNum 11
print(num) // 10
func test() {
var age = 10 {
willSet {
print("willSet", newValue)
}
didSet {
print("didSet", oldValue, age)
}
}
age = 11
// willSet 11
// didSet 10 11
}
test()
复制代码
eg:
struct Car {
static var count: Int = 0
init() {
Car.count += 1
}
}
let c1 = Car()
let c2 = Car()
let c3 = Car()
print(Car.count) // 输出3
复制代码
关于单例模式,能够参考个人另外一篇文章你真的懂单例模式么
不一样语言的单例模式,都是相似的,这里给出Swift版本单例的实现。
import Foundation
public class FileManager {
//单例模式
public static let shared = FileManager()
private init() { }
}
// 若是单例里面代码过多,能够写成以下
public class FileManager {
public static let shared = {
// ....
// ....
return FileManager()
}()
private init() { }
}
复制代码
下面的代码
import Foundation
public class YZPerson {
static var count = 3 //这里打断点
}
YZPerson.count = 6
复制代码
以下图所示,能够看出,会调用 swift_once
函数,来到这个调用位置,si汇编调试指令,一直跟进去
最终会来到这里,调用dispatch_once_f
也就是说static
内部封装了dispatch_once_
而dispatch_once_
能保证线程安全的,只能被初始化一次,因此单例的时候能够用static
关于 dispatch_once
的分析,能够看这篇文章你真的懂单例模式么
static
修饰的变量,属于全局变量。读者有兴趣本身证实。这里再也不赘述。参考资料:
更多资料,欢迎关注我的公众号,不定时分享各类技术文章。