swiftswift4xcode9-beta

Overlapping accesses to 'urlComponents', but modification requires exclusive access


I am trying to use the Lyft API with iOS 11 and Swift 4, and am receiving an error on the second line, which is

Overlapping accesses to 'urlComponents', but modification requires exclusive access; consider copying to a local variable.

I am unsure what this means, and how I can get around it. Any help is appreciated, thanks!

let queryItems = parameters
    .sorted { $0.0 < $1.0 }
    .flatMap { components(forKey: $0, value: $1) }
var urlComponents = URLComponents(url: mutableURLRequest.url!, resolvingAgainstBaseURL: false)
urlComponents?.queryItems = (urlComponents?.queryItems ?? []) + queryItems //error here

Solution

  • I guess you need to set first to a local variable and then change it , try this:

    var urlComponents = URLComponents(url: mutableURLRequest.url!, resolvingAgainstBaseURL: false) 
    var localVariable = urlComponents 
    urlComponents?.queryItems = (localVariable?.queryItems ?? []) + queryItems