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

John Harding II picture John Harding II · Sep 9, 2017 · Viewed 14.3k times · Source

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

Answer

3stud1ant3 picture 3stud1ant3 · Sep 9, 2017

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