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)
Jason Hocker
  • 6,879
  • 9
  • 46
  • 79

3 Answers3

28

This works in Xcode 8 Beta 4

    URLCache.shared = sharedCache
user6669885
  • 281
  • 2
  • 2
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
Oluwatobi Omotayo
  • 1,719
  • 14
  • 28
  • what's the diff between diskCapacity and memoryCapacity? – mfaani Jul 14 '17 at 19:36
  • 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 [here](https://stackoverflow.com/questions/46120309/when-exactly-do-things-get-removed-from-urlcaches-memory-and-disk) – mfaani Oct 22 '18 at 16:57
3

It works for Xcode 8

URLCache.shared = {
        URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
}()
Włodzimierz Woźniak
  • 3,106
  • 1
  • 26
  • 23