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
In this case the problem is the overlapping access to an optional, so the easiest solution would be to unwrap
urlComponents
before mutating itsqueryItems
:I guess you need to set first to a local variable and then change it , try this:
Might not be directly related to the question details, but for anyone googling this error message, be aware that the error also happens on attempt to manipulate with data of ambiguous type (it is unclear if it is class or a struct at the moment of manipulation).
Example when this error might appear:
Possible fix is to make it a class-only protocol, so that compiler is ensured that manipulations are always made with a class.
For before Swift 4:
In Swift 4 and later also available (and is a preferred way of doing this):