示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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( "可选绑定失败" )
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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()
}
}
|
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://blog.csdn.net/robinson_911/article/details/74578789