如何在swift 3中设置共享URLCache?

Jas*_*ker 16 nsurlcache swift3

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

let sharedCache: NSURLCache = NSURLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
NSURLCache.setSharedURLCache(sharedCache)
Run Code Online (Sandbox Code Playgroud)

小智 27

这适用于Xcode 8 Beta 4

    URLCache.shared = sharedCache
Run Code Online (Sandbox Code Playgroud)


Olu*_*ayo 12

这是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
Run Code Online (Sandbox Code Playgroud)