Swift: Ambiguous Reference to Member 'Map'

Swift: Ambiguous reference to member 'map'

Make it less ambiguous by specifying the return type with as [Int]:

myArray.map({ (number: Int) in 
if number % 2 != 0 {
return 0
} else {
return number
}
}) as [Int]

Or :

let result: [Int] = myArray.map({ (number: Int) in
if number % 2 != 0 {
return 0
} else {
return number
}
})
print(result) //[0, 2, 0, 4, 0]

As noted by vadian: The ambiguity comes from the fact that the generic return type in the closure cannot be inferred.

To understand how the compiler infers the return type of a closure, let's go back to the syntax of a closure :

let myClosure: returnType = { (params) -> returnType in
statements
}

Here, the type of myClosure is returnType. And it's set in two places: after :, and after ->. You could use type inference by removing the returnType from one of the two places, but not both.

So you could remove it from inside the curly braces (like in the code above) :

let result: [Int] = myArray.map({ (number: Int) in

Or just after the variable name, and specifying the return type of the closure inside the the curly braces:

let result = myArray.map({ (number: Int) -> Int in

Ambiguous reference to member map when attempting to append/replace array element

Unfortunately, swift is not perfect and can not infer types at all times, this is why you are getting the ambiguous reference. Try userDicts.map {val -> [String:AnyObject] in and swap out $0 for val This will explicitly tell the map that the values coming in are [String:AnyObject], and should be able to return said type

Swift ambiguous reference to member '=='

It's a misleading error message – the ambiguity actually lies with the closure expression you're passing to map(_:), as Swift cannot infer the return type of a multi-line closure without any external context.

So you could make the closure a single line using the ternary conditional operator:

let strs = things.filter { $0.id == "1"} .map { _ in
(x == 1) ? "a" : "b"
}

Or simply supply the compiler some explicit type information about what map(_:) is returning:

let strs = things.filter { $0.id == "1"} .map { _ -> String in

let strs : [String] = things.filter { $0.id == "1"} .map { _ in

Ambiguous reference to member map from concrete type

The error message is misleading. The real problem is that the map()
method applied to a dictionary does not return a new dictionary but an array, in your case [(String, String)],
See for example What's the cleanest way of applying map() to a dictionary in Swift? for a discussion of that topic.

Another problem is that NSString is not converted to String
implicitly, i.e. NSString(data: data, ...) should be replaced by
String(data: data, ...).

Using the extension method

extension Dictionary {
init(_ pairs: [Element]) {
self.init()
for (k, v) in pairs {
self[k] = v
}
}
}

from the referenced thread you can return a new dictionary with

func convert(v: AnyObject) -> [String: String] {
let dict = v as! [CBUUID: NSData]
return Dictionary(dict.map { (uuid, data) in
(uuid.UUIDString, String(data: data, encoding: NSUTF8StringEncoding) ?? "")
})
}

Alternatively, change the return type to [(String, String)]:

func convert(v: AnyObject) -> [(String, String)] {
return (v as! [CBUUID: NSData]).map { (uuid, data) in
(uuid.UUIDString, String(data: data, encoding: NSUTF8StringEncoding) ?? "")
}
}

How do I deal with Ambiguous reference to member '==' in RxBluetoothKit?

The problem is a type mismatch. The return type of function is Observable<[BluetoothState]>, but inside flatMap return type is Observable<BlePeripheral>

Ambiguous reference to member 'subscript' when using Google maps route

Use

Dictionary<String, Any>

as JSON dictionary type.

JSON keys are required to be String anyway and

AnyObject has been changed to Any in Swift 3.

Edit :

I recommend to use a type alias for the JSON dictionary type

typealias JSONObject = [String:Any] // synonym of Dictionary<String, Any>

Then you can write

var lookupAddressResults: JSONObject!
...
var selectedRoute: JSONObject!
var overviewPolyline: JSONObject!

and the parsing code

...
if status == "OK" {
let allResults = dictionary?["results"] as! Array<JSONObject>
self.lookupAddressResults = allResults[0]

// Keep the most important values.
self.fetchedFormattedAddress = self.lookupAddressResults["formatted_address"] as? String
let geometry = self.lookupAddressResults["geometry"] as! JSONObject
self.fetchedAddressLongitude = ((geometry["location"] as! JSONObject)["lng"] as! NSNumber).doubleValue
self.fetchedAddressLatitude = ((geometry["location"] as! JSONObject)["lat"] as! NSNumber).doubleValue

completionHandler(status, true)
}

...

You can also replace

( ... as! NSNumber).doubleValue

with

... as! Double

Ambiguous reference to member '==' with realm filter

I found solution and its work perfectly for me

do {
let result = try Realm().objects(Pros.self)
print(result)
let predicate = NSPredicate(format: "SELF.type == %@ AND SELF.status == 'valide' AND ANY category.type_sector = %@ AND SELF.status == 'valide'", arrType[(selectedFromType?.row)!], arrTypeSector[(selectedFromSector?.row)!])
let arrFiltered = result.filter(predicate)
print(arrFiltered)

}
catch
{
print(error)

}

flatMap and `Ambiguous reference to member` error

For this to work

let persons = records.flatMap(Person.init)

the param passed to the flatMap closure must be the same type received by Person.init, so it must be PersonRecord.

Then records must be a list of PersonRecord, something like this

let records: [PersonRecord] = []

Now it works

let persons = records.flatMap(Person.init)    

Can't sort array: Ambiguous reference to member ''

You have to declare Element to be Comparable:

extension Array where Element: Numeric & Comparable {


Related Topics



Leave a reply



Submit