Swift学习笔记5-getter & setter、ATS 应用传输安全

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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一. 常量&变量 简单体验 阶段性小结var 定义变量,设置之后可以修改let 定义常量,设置之后不可以修改语句末...
    smile丽语阅读 3,043评论 5 2
  • 本文是对 MagicalRecord github上的翻译 正文:注意: MagicalRecord 在 ARC...
    騂跃神话阅读 6,185评论 1 5
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa阅读 12,872评论 0 6
  • 这个世界上,总有那么一群人,他们或才华横溢,或聪明绝顶,他们是真正的天之骄子,他们无休止地享受着数不尽的鲜花与掌声...
    松下的瑜阅读 3,567评论 2 4
  • “清姐,回趟新加坡呗!还有3天,记得赴三年之约,紫绣球树下见!” 李耀华给我微信的时候,我披上那件驼色齐膝大风衣,...
    Smile可可阅读 4,138评论 3 2

友情链接更多精彩内容