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 中懒浮,我們通常希望在給某一個(gè)變量賦值之后蹬敲,去做一些額外的操作
- 最經(jīng)典的應(yīng)用就是在自定義 Cell 的時(shí)候赋荆,通過(guò)模型的設(shè)置方法完成 Cell 的填充
var length: Int? {
didSet {
timeStr = String(format: "%02d:%02d:%02d", arguments: [length! / 3600, (length! % 3600) / 60, length! % 60])
}
}
var timeStr: String?
計(jì)算型屬性
var title: String {
get {
return "Mr " + (name ?? "")
}
}
- 只實(shí)現(xiàn)
getter
方法的屬性被稱為計(jì)算型屬性午磁,等同于 OC 中的ReadOnly
屬性 - 計(jì)算型屬性本身不占用內(nèi)存空間
- 不可以給計(jì)算型屬性設(shè)置數(shù)值
- 計(jì)算型屬性可以使用以下代碼簡(jiǎn)寫
var title: String {
return "Mr " + (name ?? "")
}
構(gòu)造函數(shù)
init(dict: [NSObject: AnyObject]) {
name = dict["name"] as? String
age = dict["age"] as? Int
}
析構(gòu)函數(shù)
deinit {
print("88")
}
ATS 應(yīng)用傳輸安全
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.
強(qiáng)制訪問
<key>NSAppTransportSecurity</key>
<dict>
<!--Include to allow all connections (DANGER)-->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
設(shè)置白名單
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>