Beth Knight
Beth Knight

Reputation: 187

Trouble with subscript

I have a line of code that worked up until 2 months ago. Now I get the warning "ambiguous use of subscript".

Here is the line of code I'm having trouble. Was there an update in Swift recently?

if let data = response.result.value {

   let precinctNumberFromAPI = Int(data.valueForKeyPath("objects.metadata.Precinct")![0] as! String)
....

}

Upvotes: 1

Views: 53

Answers (1)

Eric Aya
Eric Aya

Reputation: 70112

With Swift 2.2 the compiler is much more strict about types, many previously inferred types are not inferred anymore if there is ambiguity.

In your case, just help the compiler know what types are your objects by safely unwrapping and casting.

Example:

if let data = response.result.value,
        array = data.valueForKeyPath("objects.metadata.Precinct") as? [String],
        precinctNumberFromAPI = array.first {
    // do stuff with precinctNumberFromAPI
}

Upvotes: 3

Related Questions