How to set the shared URLCache in swift 3?
Asked Answered
M

3

17

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)
Moguel answered 7/7, 2016 at 15:31 Comment(0)
T
28

This works in Xcode 8 Beta 4

    URLCache.shared = sharedCache
Tomato answered 2/8, 2016 at 23:24 Comment(1)
This works with final XCode 8 and should be accepted answerAssiduous
R
13

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
Rodrick answered 10/2, 2017 at 11:35 Comment(2)
what's the diff between diskCapacity and memoryCapacity?K2
Answering my question: memoryCapacity will get purged after app is terminated. Disk won't. Next time you launch, app will restore the cache from disk. For more see hereK2
H
3

It works for Xcode 8

URLCache.shared = {
        URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
}()
Haith answered 6/10, 2016 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.