How to set the shared URLCache in swift 3?

2019-04-19 01:31发布

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)

3条回答
Fickle 薄情
2楼-- · 2019-04-19 01:41

It works for Xcode 8

URLCache.shared = {
        URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
}()
查看更多
等我变得足够好
3楼-- · 2019-04-19 01:57

This works in Xcode 8 Beta 4

    URLCache.shared = sharedCache
查看更多
beautiful°
4楼-- · 2019-04-19 02:01

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
查看更多
登录 后发表回答