我正在使用 swift 进行 iOS 编程。 我正在获取用户输入的位置字符串地址并返回地理编码结果供他选择。 我遇到的问题是,当我想向用户显示地址时,我想向他显示完整的地址(名称,城市,国家)或(城市,国家)。 所以我检查并发现 ABCreateStringWithAddressDictionary 函数是获取格式化地址的标准函数。 但是,使用该函数只是给我该位置的名字(名称,城市,国家的情况下的名称)位置
cell.textLabel?.text = ABCreateStringWithAddressDictionary(returnedQueryLocations[indexPath.row].addressDictionary, true).
这不会返回完整地址。 网上找到了另一种解决方案
var lines : AnyObject = returnedQueryLocations[indexPath.row].addressDictionary[ "FormattedAddressLines"]!
var addressString : NSString = lines.componentsJoinedByString(", ")
这是在 Objective C 中,但我将其翻译为 swift。它在某种程度上有效,但在某些地址的情况下它会给我一个错误。 还有其他方法可以解决这个问题吗? ABCreateStringWithAddressDictionary 应该给出这样的结果吗?
谢谢
编辑:例如,当我搜索 Hamra,beirut,lebanon 时,它给了我(Hamra Street),第二种方法给了我所需的地址(Hamra Street, Beirut, Lebanon)。
最佳答案
ABCreateStringWithAddressDictionary() 返回的字符串使用换行符分隔其组件。文本字段仅显示第一行。在设置文本字段之前,用逗号替换换行符:
var addressString = ABCreateStringWithAddressDictionary(returnedQueryLocations[indexPath.row].addressDictionary, true)
addressString = addressString.stringByReplacingOccurrencesOfString("\n", withString: ", ")
cell.textLabel?.text = addressString
关于ios - 为什么 ABCreateStringWithAddressDictionary 没有给出详细名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31307673/