gpt4 book ai didi

arrays - Swift 1.2 按关键字过滤结构数组

转载 作者:搜寻专家 更新时间:2023-10-30 23:00:04 26 4
gpt4 key购买 nike

我需要一些帮助来过滤结构数组。

这就是我目前正在做的,它过滤了数组但不正确。例如,假设我在数组中搜索带有“Mid”的项目我有一个应该显示的项目但是显示的项目以“Bad”开头。

    var array = breweries.filter() { $0.name?.lowercaseString.rangeOfString(searchController.searchBar.text.lowercaseString) != nil }

results = array

这是我的结构

struct Breweries {
let name: String?
let breweryId: String?
let distance: Double?
let largeIconURL: String?
let streetAddress: String?
let locality: String?
let region: String?
let phone: String?
let website: String?

init(brewDictionary: [String: AnyObject]) {

name = brewDictionary["brewery"]?["name"] as? String
breweryId = brewDictionary["breweryId"] as? String
distance = brewDictionary["distance"] as? Double
largeIconURL = brewDictionary["brewery"]?["images"]??.objectForKey("large") as? String
streetAddress = brewDictionary["streetAddress"] as? String
locality = brewDictionary["locality"] as? String
region = brewDictionary["region"] as? String
phone = brewDictionary["phone"] as? String
website = brewDictionary["website"] as? String


}
}

请指正方向!

注意:我使用的是 Swift 1.2

更新:

我认为视频有助于更好地解释我正在尝试做的事情。

Demo Of issue

我想要的是找到数组的过滤器,以便只显示具有相似名称的项目。

更新 2:事实证明,当我的 UISearchController 处于事件状态时,我忘记处理这种情况。

最佳答案

假设您的 Struct 名称是 Breweries 并且它有一个 name 属性,试试这个:

let array = breweries.filter() {
($0.name!.lowercaseString as NSString).containsString(searchController.searchBar.text.lowercaseString)
}

关于arrays - Swift 1.2 按关键字过滤结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31571201/

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