【问题标题】:How to set the shared URLCache in swift 3?如何在 swift 3 中设置共享 URLCache?
【发布时间】:2016-11-10 00:20:30
【问题描述】:

这是我们在 Swift 2 中的代码。什么是 Swift 3 版本?我没有看到 setShared 的替代品。

let sharedCache: NSURLCache = NSURLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
NSURLCache.setSharedURLCache(sharedCache)

【问题讨论】:

    标签: swift3 nsurlcache


    【解决方案1】:

    这适用于 Xcode 8 Beta 4

        URLCache.shared = sharedCache
    

    【讨论】:

    • 这适用于最终的 XCode 8,应该被接受的答案
    【解决方案2】:

    适用于 Xcode 8

    URLCache.shared = {
            URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
    }()
    

    【讨论】:

      【解决方案3】:

      这是 Swift 3 中将缓存大小增加到 500 MB 的示例

          let memoryCapacity = 500 * 1024 * 1024
          let diskCapacity = 500 * 1024 * 1024
          let cache = URLCache(memoryCapacity: memoryCapacity, diskCapacity: diskCapacity, diskPath: "myDataPath")
          URLCache.shared = cache
      

      【讨论】:

      • diskCapacity 和 memoryCapacity 有什么区别?
      • 回答我的问题:应用程序终止后,memoryCapacity 将被清除。磁盘不会。下次启动时,应用程序将从磁盘恢复缓存。有关更多信息,请参阅here
      猜你喜欢
      • 1970-01-01
      • 2017-10-04
      • 2017-11-03
      • 2018-01-25
      • 2017-01-30
      • 2017-04-09
      • 2017-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多