How to set the shared URLCache in swift 3?

天大地大妈咪最大 提交于 2019-12-22 01:39:08

问题


This is the code we had in Swift 2. What is the Swift 3 version? I don't see a replacement for setShared.

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

回答1:


This works in Xcode 8 Beta 4

    URLCache.shared = sharedCache



回答2:


Here is an Example in Swift 3 increasing cache size to 500 MB

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



回答3:


It works for Xcode 8

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


来源:https://stackoverflow.com/questions/38249573/how-to-set-the-shared-urlcache-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!