getter & setter
自定义 Person 类
class Person: NSObject {
var name: String?
var age: Int?
}
getter & setter
var _name: String?
var name: String? {
get {
return _name
}
set {
_name = newValue
}
}
- 在
Swift
中以上形式的 getter & setter 很少用
didSet
- 在 OC 中,我们通常希望在给某一个变量赋值之后,去做一些额外的操作
- 最经典的应用就是在自定义 Cell 的时候,通过模型的设置方法完成 Cell 的填充
var length: Int? {
didSet {
timeStr = String(format: "%02d:%02d:%02d", arguments: [length! / 3600, (length! % 3600) / 60, length! % 60])
}
}
var timeStr: String?
计算型属性
var title: String {
get {
return "Mr " + (name ?? "")
}
}
- 只实现
getter
方法的属性被称为计算型属性,等同于 OC 中的ReadOnly
属性 - 计算型属性本身不占用内存空间
- 不可以给计算型属性设置数值
- 计算型属性可以使用以下代码简写
var title: String {
return "Mr " + (name ?? "")
}
构造函数
init(dict: [NSObject: AnyObject]) {
name = dict["name"] as? String
age = dict["age"] as? Int
}
析构函数
deinit {
print("88")
}
ATS 应用传输安全
App Transport Security (ATS) lets an app add a declaration to its Info.plist file that specifies the domains with which it needs secure communication. ATS prevents accidental disclosure, provides secure default behavior, and is easy to adopt. You should adopt ATS as soon as possible, regardless of whether you’re creating a new app or updating an existing one. <br /><br />If you’re developing a new app, you should use HTTPS exclusively. If you have an existing app, you should use HTTPS as much as you can right now, and create a plan for migrating the rest of your app as soon as possible.
强制访问
<key>NSAppTransportSecurity</key>
<dict>
<!--Include to allow all connections (DANGER)-->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
设置白名单
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>