我正在为 iOS 编写一个应用程序,我在其中搜索具有特定 categoryId 的场所,而不是搜索查询。我的应用程序不需要用户登录,所以我使用了名为 das-quadrat 的第三方 API 包装器与用户跳过 OAuth 的麻烦。 我正在关注页面底部的搜索请求示例。
此外,这是他们在演示项目 (ExploreViewController.swift) 中实现其功能的方式:
func exploreVenues() {
let location = self.locationManager.location
var parameters = location.parameters()
let task = self.session.venues.explore(parameters) {
(result) -> Void in
if self.venueItems != nil {
return
}
if !NSThread.isMainThread() {
fatalError("!!!")
}
if result.response != nil {
if let groups = result.response!["groups"] as? [[String: AnyObject]] {
var venues = [[String: AnyObject]]()
for group in groups {
if let items = group["items"] as? [[String: AnyObject]] {
venues += items
}
}
self.venueItems = venues
}
self.tableView.reloadData()
} else if result.error != nil && !result.isCancelled() {
self.showErrorAlert(result.error!)
}
}
task.start()
}
在我自己的代码中,我有一些相关的 categoryId 数组,我想对它们中的每一个执行搜索任务。在这里,我尝试搜索 categoryIds,然后将 field 名称和与用户的距离存储在字典中(使用 SwiftyJSON - 我会链接,但我还没有足够的声誉)从 JSON 中获取元素:
var venues: [JSONParameters]!
var distance: [JSONParameters]!
var session : Session!
let tagData = TagData()
/*
TagData contains a dictionary with relevant categoryIDs and the number
of times the user has visited that venue.
*/
var categoryTagSearch: [String] = TagData.returnRelevantCategories()
var venueNamesDictionary: [String: Int] = [:] //name : distance from user
func findVenues(callback: [String: Int] -> Void) {
for tag in categoryTagSearch {
let searchTask = session.venues.search(tag) {
(result) -> Void in
if let response = result.response { //trying to create keys of names with values of distances
let count: Int? = response["venues"].array?.count
let venueName: [String] = response["venues"]["name"].arrayValue
let distanceValue: [Int] = response["venues"]["location"]["distance"].arrayValue
for (i = 0; i < count; i++) {
venueNamesDictionary[venueName[i]] = distanceValue[i]
}
}
} // end searchtask assignment
callback(searchTask.start()) //to force this to wait for responses to finish filling Dictionary
}// end for
}// end Function
问题是搜索一个categoryId很少返回搜索结果;这类似于在 Foursquare 的搜索栏中输入“4bf58dd8d48988d1c8941735”,它要么不会返回正确类别的地点,要么根本不会返回任何内容。 如何确保我的搜索只返回正确类别的场所?是否有我忽略的功能?
最佳答案
你真的指定类别吗?要指定类别,请执行以下操作。
var parameters = [Parameter.categoryId:"4bf58dd8d48988d1c8941735"]
currentTask = session.venues.search(parameters) {
关于ios - 在 Foursquare "Das-Quadrat"iOS API 包装器中使用 categoryId 作为搜索过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31707955/