swift3.0 可选绑定共用同一块内存空间

前端之家收集整理的这篇文章主要介绍了swift3.0 可选绑定共用同一块内存空间前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


       ljTempModel = UserModel.init(userName: "sww",userID: 12,phone: "123",email: "deew")
        ljTempModel?.ljArray.append("sww")
        print("可选绑定前:\(ljTempModel?.ljArray)")
        
        //可选绑定成功,然后赋值
        if let modle : UserModel = ljTempModel{
            
            modle.ljArray.append("123")
            
            print("可选绑定后:\(String(describing: ljTempModel?.ljArray))")

            print("\(modle.ljArray)可选绑定成功,然后赋值")
           //此处modle 和ljTempModel有相同的内存地址空间,所以对modle添加“123”,就相当于对ljTempModel添加“123”,实际打印结果也是如此
        }else{
            //绑定失败
            print("可选绑定失败")
        }
        
    }

import Foundation


open class UserModel : NSObject {
    var userName: String     ///< store user's name,optional
    var userID: Int          ///< store user's ID
    var phone: String?       ///< store user's telephone number
    var email: String?       ///< store user's email
    var responseData = NSMutableData()
    var ljArray = Array<Any>()
    
    
    // designated initializer
    init(userName: String,userID: Int,phone: String?,email: String?) {
        self.userName = userName
        self.userID = userID
        self.phone = phone
        self.email = email
        
        super.init()
    }
}

原文链接:https://www.f2er.com/swift/321481.html

猜你在找的Swift相关文章