详解 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()
 }
}

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

02-09 11:04