Search code examples
swiftnsurlrequest

NSMutableURLRequest cachePolicy not working


Here's my code

    let url = NSURL(string: user_url)
    var request = NSMutableURLRequest.init(URL: url!, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 5)

I'm getting this error:

Type 'NSURLRequestCachePolicy' has no member 'reloadIgnoringLocalCacheData'

I pulled this constant from the docs so I am wondering where I am going wrong here?

This is for Swift 2.3 (not much choice/decision) + XCode 8.


Solution

  • For Swift 2.x you want:

    let url = NSURL(string: user_url)
    var request = NSMutableURLRequest.init(URL: url!, cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: 5)
    

    ReloadIgnoringLocalCacheData was renamed to reloadIgnoringLocalCacheData as part of the great Swift 3 renaming, hence the documentation. To find out what the compiler is looking for, command-click on the method call, and then on the parameter type (NSURLRequestCachePolicy) -- that will take you to the declaration that the compiler is actually using.