gpt4 book ai didi

json - UISearchBar 和 JSON 数据

转载 作者:行者123 更新时间:2023-11-28 12:09:10 25 4
gpt4 key购买 nike

这是我第一次使用 UISearchBar,事实证明它让我感到困惑。

我认为我最大的问题是不理解 JSON 是如何存储和显示的。

我的 JSON 数据存储为:

    [{
"referralId" : "v-1519014616",
"name" : "Empire State Building",
"storeId" : "",
"hereNow" : {
"count" : 2,
"summary" : "2 people are here",
"groups" : [
{
"count" : 2,
"type" : "others",
"name" : "Other people here",
"items" : [

]
}
]
},
"stats" : {
"tipCount" : 1105,
"checkinsCount" : 185916,
"usersCount" : 129661
},
"venueRatingBlacklisted" : true,
"beenHere" : {
"lastCheckinExpiredAt" : 0
},
"specials" : {
"count" : 0,
"items" : [

]
},
"venuePage" : {
"id" : "64514349"
},
"verified" : true,
"location" : {
"state" : "NY",
"neighborhood" : "Midtown Manhattan, New York, NY",
"crossStreet" : "btwn 33rd & 34th St",
"lat" : 40.748469532965927,
"address" : "350 5th Ave",
"cc" : "US",
"city" : "New York",
"postalCode" : "10118",
"formattedAddress" : [
"350 5th Ave (btwn 33rd & 34th St)",
"New York, NY 10118"
],
"lng" : -73.985513794430119,
"distance" : 17,
"country" : "United States"
},
"hasPerk" : false,
"id" : "43695300f964a5208c291fe3",
"categories" : [
{
... etc
},

我将其存储在一个名为 locations 的数组中,我正尝试使用搜索栏对其进行过滤。

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let name = self.locations[0]["name"].string
filteredLocations = locations.filter { names in
return (name!.lowercased().contains(searchText.lowercased()))
}

tableView.reloadData()
}

现在,很明显,name 只会搜索数组中的项目。我如何才能过滤每个位置的 name 键以正确过滤?

最佳答案

试试这个 ...不要先从位置数组中取名字...

 let filterArr = locations.filter {
return (($0["name"] as! String).lowercased().contains(searchText.lowercased())))
}

使用这个来获取搜索结果

关于json - UISearchBar 和 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48859600/

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