在swift中对成员’joinWithSeparator’的模糊引用
发布时间:2020-12-14 05:50:00 所属栏目:百科 来源:网络整理
导读:我正在使用Google的’reverseGeocodeCoordinate’来获取基于纬度和经度的地址。 我在实现中遇到以下错误 Ambiguous reference to member ‘joinWithSeparator’ 以下是我的实施: let aGMSGeocoder: GMSGeocoder = GMSGeocoder()aGMSGeocoder.reverseGeocode
我正在使用Google的’reverseGeocodeCoordinate’来获取基于纬度和经度的地址。
我在实现中遇到以下错误
以下是我的实施: 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)") } 我正在尝试使用数组’gmsAddress.lines’中的元素创建一个addressString,但最终会出现错误消息。 实现了一些示例代码片段来测试’joinWithSeparator’ let sampleArray = ["1","2","3","4","5"] let joinedString = sampleArray.joinWithSeparator("") print("joinedString=(joinedString)") 它成功了。 /** An array of NSString containing formatted lines of the address. May be nil. */ public var lines: [AnyObject]! { get } 那么,在没有循环数组的情况下,实现以下行的可能方法有哪些: let addressString = gmsAddress.lines.joinWithSeparator("")
它是不明确的,因为数组可以包含AnyObject,这意味着数组中的每个对象都可以是不同的类型。因此,编译器无法预先知道是否可以连接数组中的任何两个对象。
sampleArray工作的原因是它已被隐式确定为字符串数组。 如果您知道lines数组中的每个元素都是一个字符串,您可以强制将它向下转换为字符串数组: let addressString = (gmsAddress.lines as! [String]).joinWithSeparator("") 虽然这可能是值得安全的,并先检查。 if let lines = gmsAddress.lines as? [String] { let addressString = lines.joinWithSeparator(",") ... } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |