详解 swift3.0 可选绑定共用同一块内存空间的实例
程序员文章站
2023-12-20 19:24:04
详解 swift3.0 可选绑定共用同一块内存空间的实例
示例代码:
ljtempmodel = usermodel.init(username: "sww",...
详解 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() } }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!