Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 910
John Harding II Avatar asked Sep 09 '17 02:09

John Harding II


3 Answers

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  
like image 123
3stud1ant3 Avatar answered Nov 20 '22 05:11

3stud1ant3


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
    // ...
}
like image 41
Martin R Avatar answered Nov 20 '22 05:11

Martin R


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 }
}
like image 2
Vitalii Avatar answered Nov 20 '22 05:11

Vitalii