Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the shared URLCache in swift 3?

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)
like image 412
Jason Hocker Avatar asked Jul 07 '16 15:07

Jason Hocker


3 Answers

This works in Xcode 8 Beta 4

    URLCache.shared = sharedCache
like image 130
user6669885 Avatar answered Nov 01 '22 23:11

user6669885


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
like image 32
Oluwatobi Omotayo Avatar answered Nov 01 '22 23:11

Oluwatobi Omotayo


It works for Xcode 8

URLCache.shared = {
        URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
}()
like image 3
Włodzimierz Woźniak Avatar answered Nov 01 '22 22:11

Włodzimierz Woźniak