在swift中对成员’joinWithSeparator’的模糊引用

在swift中对成员’joinWithSeparator’的模糊引用,第1张

概述我正在使用Google的’reverseGeocodeCoordinate’来获取基于纬度和经度的地址。 我在实现中遇到以下错误 Ambiguous reference to member ‘joinWithSeparator’ 以下是我的实施: let aGMSGeocoder: GMSGeocoder = GMSGeocoder()aGMSGeocoder.reverseGeocodeCoo 我正在使用Google的’reverseGeocodeCoordinate’来获取基于纬度和经度的地址。
我在实现中遇到以下错误

Ambiguous reference to member ‘joinWithSeparator’

以下是我的实施:

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)")

它成功了。
我观察到的是,’sampleArray’是String类型的元素数组,但’gmsAddress.lines’是’AnyObject’类型的元素数组,在’GMSAddress’库中找到:

/** 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(",")    ...}
总结

以上是内存溢出为你收集整理的在swift中对成员’joinWithSeparator’的模糊引用全部内容,希望文章能够帮你解决在swift中对成员’joinWithSeparator’的模糊引用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1051981.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存