我当前使用来自 Google Places API 端点的地址填充 tableView 的代码如下:
但是,我想创建一个自定义客户端来处理由 Places 提供的 autoCompleteQuery 手动处理的功能。我假设这要求再次解析地址 JSON,并进行迭代,然后将其存储在数组中。如果您有解决方案,请告诉我。注释代码完美运行,我正在尝试手动实现相同的结果。
func searchBar(searchBar: UISearchBar, textDidChange searchText: String){
// let mapsClient = GMSPlacesClient()
// mapsClient.autocompleteQuery(searchText, bounds: nil, filter: nil {(results, error: NSError?) in
//
// self.resultsArray.removeAll()
//
// if results == nil{
// return
// }
//
// for result in results! {
//
// if let result = result as GMSAutocompletePrediction! {
// self.resultsArray.append(result.attributedFullText.string)
// }
// }
//
// self.searchResultsClient.reloadDataWithArray(self.resultsArray)
// }
gmsFetcher?.sourceTextHasChanged(searchText)
self.searchResultsClient.reloadDataWithArray(self.resultsArray)
print(resultsArray)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension MainMapViewController: GMSAutocompleteFetcherDelegate {
func didAutocompleteWithPredictions(predictions: [GMSAutocompletePrediction]) {
self.resultsArray.count + 1
let resultsStr = NSMutableString()
for prediction in predictions {
resultsStr.appendFormat("%@\n", prediction.attributedPrimaryText)
}
resultText?.text = resultsStr as String
self.resultsArray.append(resultsStr as String)
self.searchResultsClient.reloadDataWithArray(self.resultsArray)
}
func didFailAutocompleteWithError(error: NSError) {
resultText?.text = error.localizedDescription
}
}
最佳答案
您可以使用 GMSAutocompleteFetcher,它将 autocompleteQuery 方法包装在 GMSPlacesClient 上。 getter 会限制请求,仅返回最近输入的搜索文本的结果,并且不提供任何 UI 元素。
实现 GMSAutocompleteFetcher 的步骤:
- Implement the
GMSAutocompleteFetcherDelegateprotocol.- Create a
GMSAutocompleteFetcherobject.- Call
sourceTextHasChangedon the fetcher as the user types.- Handle predictions and errors using the
didAutcompleteWithPredictionsanddidFailAutocompleteWithErrorprotocol methods.
可以在 Use the fetcher 中找到演示使用提取器实现步骤的示例代码。 .
关于ios - 试图复制 GMSPlacesClient : autoCompleteQuery function to populate a UITableView manually,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36640502/
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上
我想编写一个ruby脚本来递归复制目录结构,但排除某些文件类型。因此,给定以下目录结构:folder1folder2file1.txtfile2.txtfile3.csfile4.htmlfolder2folder3file4.dll我想复制这个结构,但不包含.txt和.cs文件。因此,生成的目录结构应如下所示:folder1folder2file4.htmlfolder2folder3file4.dll 最佳答案 您可以使用查找模块。这是一个代码片段:require"find"ignored_extensions=[".cs"
之前有人问过这个问题,我发现了以下clip关于如何一次设置一个类对象的所有属性,但由于批量分配保护,这在Rails中是不可能的。(例如,您不能Object.attributes={})有没有一种很好的方法可以将一个类的属性合并到另一个类中?object1.attributes=object2.attributes.inject({}){|h,(k,v)|h[k]=vifObjectModel.column_names.include?(k);h}谢谢。 最佳答案 利用assign_attributes使用:without_prote
(跟进我之前的问题,Ruby:howcanIcopyavariablewithoutpointingtothesameobject?)我正在编写一个简单的Ruby程序来在.svg文件中进行一些替换。第一步是从文件中提取信息并将其放入数组中。为了避免每次调用此函数时都从磁盘读取文件,我尝试使用memoize设计模式-在第一次调用后的每次调用中都使用缓存结果。为此,我使用了一个在函数之前定义的全局变量。但是,即使我在返回局部变量之前将该变量.dup为局部变量,调用该变量的函数仍在修改全局变量。这是我的实际代码:#memoizetokeepfromhavingtoreadoriginalfi
我不是Ruby专家,但想弄清楚发生了什么,因为我试图让指南针在节点应用程序中工作,但我的Ruby似乎坏了。打字:ruby--version让我:ruby2.1.1p76(2014-02-24revision45161)[x86_64-darwin13.0]我安装了Homebrew,之前遇到过Ruby版本的问题,但它似乎已安装并且可以正常工作。但是,当我使用gem输入请求时,出现此错误:$gem-hErrorloadingRubyGemsplugin"/Users/user_dir/.rvm/gems/ruby-2.1.1@global/gems/executable-hooks-1.3
当我将IO::popen与不存在的命令一起使用时,我在屏幕上打印了一条错误消息:irb>IO.popen"fakefake"#=>#irb>(irb):1:commandnotfound:fakefake有什么方法可以捕获此错误,以便我可以在脚本中进行检查? 最佳答案 是:升级到ruby1.9。如果您在1.9中运行它,则会引发Errno::ENOENT,您将能够拯救它。(编辑)这是在1.8中的一种hackish方式:error=IO.pipe$stderr.reopenerror[1]pipe=IO.popen'qwe'#
当我尝试使用“套接字”库中的方法“read_nonblock”时出现以下错误IO::EAGAINWaitReadable:Resourcetemporarilyunavailable-readwouldblock但是当我通过终端上的IRB尝试时它工作正常如何让它读取缓冲区? 最佳答案 IgetthefollowingerrorwhenItrytousethemethod"read_nonblock"fromthe"socket"library当缓冲区中的数据未准备好时,这是预期的行为。由于异常IO::EAGAINWaitReadab