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
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
In this case the problem is the overlapping access to an optional,
so the easiest solution would be to unwrap urlComponents
before
mutating its queryItems
:
if var urlComponents = URLComponents(url: mutableURLRequest.url!, resolvingAgainstBaseURL: false) {
urlComponents.queryItems = (urlComponents.queryItems ?? []) + queryItems
// ...
}
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:
protocol: AnItemCapableToShowDetails {
var isShowingDetails: Bool { get set }
}
class: DataItem, AnItemCapableToShowDetails {
...
var isShowingDetails = false
}
class: SomeClass {
func showDetails() {
if let dataItem = itemsArray[index] as? AnItemCapableToShowDetails {
...
dataItem.isShowingDetails = !dataItem.isShowingDetails // <- "Overlapping accesses..." error here
// because that protocol might
// potentially be applied to a struct as well
}
}
}
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:
protocol: AnItemCapableToShowDetails: class {
var isShowingDetail: Bool { get set }
}
In Swift 4 and later also available (and is a preferred way of doing this):
protocol: AnItemCapableToShowDetails: AnyObject {
var isShowingDetail: Bool { get set }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With