Usage value and reference type (I) (see pending)

class SwiftClass {
    var name: String?
    var height = 0.0
    var width = 0.0
    
    var description: String {
        return "ResolutionClass(height: \(height), width: \(width))"
    }
    
    func printString(alert: String) -> Void {
        print("\(alert)")
    }
}

struct SwiftStruct {
    var height = 0.0
    var width = 0.0
}


引用类型使用intout参数,意义不大

func swap(clss: inout SwiftClass) {
    //打印引用类型变量指向的内存地址
    print("During calling: \(Unmanaged.passUnretained(clss).toOpaque())")
    let temp = clss.height
    clss.height = clss.width
    clss.width = temp
}


sc.height = 1080
sc.width = 1920
print(sc)
print("Before calling: \(Unmanaged.passUnretained(sc).toOpaque())")
swap(clss: &sc)
print(sc)
print("After calling: \(Unmanaged.passUnretained(sc).toOpaque())")

print:
SwiftTest.SwiftClass
Before calling: 0x000000010284b5d0
During calling: 0x000000010284b5d0
SwiftTest.SwiftClass
After calling: 0x000000010284b5d0

使用intout注意事项:

  • Use the keyword inout function, you need to add an ampersand in front of the parameter when you call;
  • inout parameters must be passed at a variable, not be a constant or literal (literal);
//常量使用关键字 let 来声明
格式:let constantName = <initial value>
如:let constA = 42

//字面量:就是指能够直接了当地指出自己的类型并为变量进行赋值的值,与常量无异。
//字符串型字面常量
let name = "DevZhang"

  • inout parameters can have default values, variable parameters can not
//可变参数,有多个参数用省略号表示
func add(a:Int, b:Int ,others:Int ...) -> Int {
var result = a + b
for num in others {
    result += num
}
    return result
}

let number = add(2, b: 5, others: 2, 50, 4)
print(number)  //63
  • inout parameters is not equivalent to the return value of the function, that the scope parameter is a function of ways beyond the body
  • Inout parameters can not pass a plurality of the same variable, since the copy-in the order of adventitious copy, then the final value can not be determined


struct Point {
    var x = 0.0
    var y = 0.0
}

struct Rectangle {
    var width = 0.0
    var height = 0.0
    var origin = Point()
    
    var center: Point {
        get {
            print("center GETTER call")
            return Point(x: origin.x + width / 2,
                         y: origin.y + height / 2)
        }
        
        set {
            print("center SETTER call")
            origin.x = newValue.x - width / 2
            origin.y = newValue.y - height / 2
        }
    }
    
    func reset(center: inout Point) {
        center.x = 0.0
        center.y = 0.0
    }
    
}

var rect = Rectangle(width: 100, height: 100, origin: Point(x: -100, y: -100))
print("rect.center 值:\(rect.center)\n")
rect.reset(center: &rect.center)
print("rect.center 重置后的值:\(rect.center)")

print:
center GETTER call
rect.center 值:Point(x: -50.0, y: -50.0)

center GETTER call
center SETTER call
center GETTER call
rect.center 重置后的值:Point(x: 0.0, y: 0.0)

inout 参数传递过程

  • When the function is called, the parameter values ​​are copied
  • In the body of the function, the copied parameter modification
  • When the function returns, parameter values ​​are assigned to the copied original variable

  Officials say this behavior is: copy-in copy-out or call by value result. We can use KVO or calculated attribute to track the process, in order to calculate the property as an example here. Exclude center GETTER call later, which can be found before calling functions: the first parameter value is obtained (setter is called), then set value (setter being called).

  The transfer process inout parameters can be known: the nature inout parameter reference type of transmission parameters and not the same thing. inout parameters broke its life cycle, it is a shallow copy. In Swift 3.0, are also captured in the completely escape aside the closure (Escape Closure) in.




Nested types

值类型嵌套值类型


值类型嵌套引用类型


引用类型嵌套值类型


``


















References: [1]


  1. value and reference types swift

Reproduced in: https: //www.jianshu.com/p/04f955f3c5c0

Guess you like

Origin blog.csdn.net/weixin_33724059/article/details/91279528