gpt4 book ai didi

swift - 带有组合框的 mapkit 自动完成

转载 作者:行者123 更新时间:2023-11-28 08:02:57 27 4
gpt4 key购买 nike

早上好

我在 osx 上使用 swift 4,我试图实现一个“地址自动完成组合框”

这意味着:用户开始输入地址,组合框应该显示可能的地址:

我试过这个:

@IBOutlet weak var cbSourceAddress: NSComboBox!
var searchCompleter = MKLocalSearchCompleter()
var searchResults = [MKLocalSearchCompletion]()
var cbData = [String]()


func numberOfItems(in comboBox: NSComboBox) -> Int {
return cbData.count
}


func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
return cbData[index] as AnyObject
}



// 1
func comboBox(_ comboBox: NSComboBox, completedString string: String) -> String? {

searchCompleter.queryFragment = cbSourceAddress.stringValue

for data in cbData {
if string.characters.count < data.characters.count{
let statePartialStr = data.lowercased()[data.lowercased().startIndex..<data.lowercased().index(data.lowercased().startIndex, offsetBy: string.characters.count)]
if statePartialStr.range(of: string.lowercased()) != nil {
print(data)
return data
}
}
}
return ""
}


// 2
func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
searchResults = completer.results


for x in 0 ..< completer.results.count {
print(searchResults[x])
cbData.append("\(searchResults[x])")
}
cbSourceAddress.reloadData()
}

我的打印结果:

<MKLocalSearchCompletion 0x60000105f350> New England Aquarium (1 Central Wharf, Boston, MA 02110-3309, United States)
<MKLocalSearchCompletion 0x6000012412f0> New England Lobster Market & Eatery (824 Cowan Rd, Burlingame, CA 94010, United States)
<MKLocalSearchCompletion 0x600001240f00> England-Idlewild Park and Dog Park (5550 Idlewild Rd, Burlington, KY 41005, United States)
<MKLocalSearchCompletion 0x60000105ecc0> Best Western Plus New Englander (1 Rainin Rd, Woburn, MA 01801, United States)
<MKLocalSearchCompletion 0x6000008408a0> Melbourne Grammar School - Wadhurst Campus (355 St Kilda Road, St Kilda Road VIC 3004, Australia)

如何格式化为“人工优化的字符串”?我的方法是实现自动完成过程的正确方法吗?

最佳答案

好的,我找到了解决方案:print(searchResults[x].title)

关于swift - 带有组合框的 mapkit 自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46070342/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com