I'm using Google's 'reverseGeocodeCoordinate
' to get address based on latitude and longitude.
I'm getting the following error in the implementation
Ambiguous reference to member 'joinWithSeparator'
Below is my implementation:
let aGMSGeocoder: GMSGeocoder = GMSGeocoder()
aGMSGeocoder.reverseGeocodeCoordinate(CLLocationCoordinate2DMake(17.45134626, 78.39304448)) {
(let gmsReverseGeocodeResponse: GMSReverseGeocodeResponse!, let error: NSError!) -> Void in
let gmsAddress: GMSAddress = gmsReverseGeocodeResponse.firstResult()
print("lines=\(gmsAddress.lines)")
let addressString = gmsAddress.lines.joinWithSeparator("")
print("addressString=\(addressString)")
}
I'm trying to create a addressString
with the elements in array 'gmsAddress.lines
', but end up with an error message.
Implemented some sample snippet to test 'joinWithSeparator
'
let sampleArray = ["1", "2", "3", "4", "5"]
let joinedString = sampleArray.joinWithSeparator("")
print("joinedString=\(joinedString)")
It succeeded.
What I observe is, 'sampleArray
' is an array of elements of type String
, but 'gmsAddress.lines
' is an array of elements of type 'AnyObject
', Found in 'GMSAddress
' library:
/** An array of NSString containing formatted lines of the address. May be nil. */
public var lines: [AnyObject]! { get }
So, What are the possible ways to achieve the following line without looping the array:
let addressString = gmsAddress.lines.joinWithSeparator("")
It is ambiguous because the array can contain AnyObject
meaning every object in the array could be of a different type. Therefore the compiler can't know in advance whether any two objects in the array can be joined.
The reason your sampleArray
works is that it has been implicitly determined to be an array of strings.
If you know for a fact that every element in the lines
array is a string you can force downcast it to an array of strings:
let addressString = (gmsAddress.lines as! [String]).joinWithSeparator("")
Though it's probably worthwhile being safe about this and checking first.
if let lines = gmsAddress.lines as? [String] {
let addressString = lines.joinWithSeparator(", ")
...
}