swift如何打印對象的地址

打印對象的地址還是有着很多實用價值的,在swift中,你可以用以下的方式打印一個對象的地址:

打印結果:

有時候,if let a = b 這種操作會給人一種錯覺,認為 a 是臨時創建出來的一個變量,其實,他只不過是指針而已:

源碼:
// // ViewController.swift // SwiftDemo // // Created by YouXianMing on 15/10/14. // Copyright © 2015年 YouXianMing. All rights reserved. // import UIKit class ViewController: UIViewController { var model : Model! override func viewDidLoad() { super.viewDidLoad() model = Model() if let tmpModel = model { print(unsafeAddressOf(tmpModel)) print(unsafeAddressOf(model)) } let tmpModel = model print(unsafeAddressOf(tmpModel)) } }
// // Model.swift // SwiftDemo // // Created by YouXianMing on 15/10/14. // Copyright © 2015年 YouXianMing. All rights reserved. // import UIKit class Model: NSObject { }
