我正在应用程序中使用产品搜索引擎。
我有此代码:
func searchStringInProductArray(searchString: String) {
let documentsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let path = documentsDir.appendingPathComponent((AppGlobalManager.sharedManager.loggedUser?.selectedLanguage)! + "/json/products.json")
do {
let data = try Data(contentsOf: path)
let decoder = JSONDecoder()
var productsTmpObjectArray = [Products]()
productsTmpObjectArray = try decoder.decode([Products].self, from: data)
productsTmpObjectArray = productsTmpObjectArray.filter({($0.code?.contains(searchString))!})
productsTmpObjectArray = productsTmpObjectArray.filter({($0.name?.contains(searchString))!})
productsTmpObjectArray = productsTmpObjectArray.filter({($0.langVersions.name?.contains(searchString))!})
productsTmpObjectArray = productsTmpObjectArray.filter({($0.langVersions?.description?.contains(searchString))!})
productsObjectArray = productsTmpObjectArray
collectionView1.reloadData()
} catch {
NotificationCenter.default.post(name: Notification.Name("NotificationAlertMessage"), object: nil, userInfo: ["object": errorMessage(title: "Blad".localized(), description: "problemWithParseData".localized(), buttonTitle: "Ok".localized())])
}
}
和对象:
struct ChangeTime : Codable {
let year : Int?
let month : Int?
let dayOfMonth : Int?
let hourOfDay : Int?
let minute : Int?
let second : Int?
}
struct Lang : Codable {
let id : Int?
let code : String?
let name : String?
}
struct LangVersions : Codable {
let id : Int?
let parentId : Int?
let name : String?
let lang : Lang?
let description : String?
}
struct Products : Codable {
let id : Int?
let code : String?
let name : String?
let inPieces : Bool?
let prepDeepFryer : String?
let langVersions : [LangVersions]?
let changeTime : ChangeTime?
}
在TextField中输入文本后将调用该函数:
@IBAction func searchProductTextFieldChanged(_ sender: Any) {
searchStringInProductArray(searchString: searchTextField.text!)
}
我有以下错误:
productsTmpObjectArray = productsTmpObjectArray.filter({($ 0.langVersions.name?.contains(searchString))!})-类型'[LangVersions]?'的值没有成员“姓名”
productsTmpObjectArray = productsTmpObjectArray.filter({($ 0.langVersions?.description?.contains(searchString))!})-无法对类型为'String'的非可选值使用可选链接
< / li>我希望我的搜索引擎一次搜索所有字段:
$ 0.code?.contains(searchString)
$ 0.name?.contains(searchString)
$ 0.langVersions.name?.contains(searchString)
$ 0.langVersions?.description?包含(searchString)
答案 0 :(得分:2)
您还没有注意到'Products'对象包含'LangVersions'对象的数组。方法的编辑版本:
func searchStringInProductArray(searchString: String) {
let documentsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let path = URL(string: "")!
do {
let data = try Data(contentsOf: path)
let decoder = JSONDecoder()
var productsTmpObjectArray = [Products]()
productsTmpObjectArray = try decoder.decode([Products].self, from: data)
productsObjectArray = productsTmpObjectArray.filter({return ($0.code?.lowercased().contains(searchString.lowercased()))! || ($0.name?.lowercased().contains(searchString.lowercased()))! || (($0.langVersions?.filter({($0.name?.lowercased().contains(searchString.lowercased()))!}))?.count)! > 0 || (($0.langVersions?.filter({($0.description?.lowercased().contains(searchString.lowercased()))!}))?.count)! > 0 })
collectionView1.reloadData()
} catch {
NotificationCenter.default.post(name: Notification.Name("NotificationAlertMessage"), object: nil, userInfo: ["object": errorMessage(title: "Blad".localized(), description: "problemWithParseData".localized(), buttonTitle: "Ok".localized())])
}
}
答案 1 :(得分:1)
您可以尝试搜索所有值
let searchString = "searStr".lowercased()
productsTmpObjectArray = productsTmpObjectArray.filter { ($0.name?.lowercased().contains(searchString))! || ($0.code?.lowercased().contains(searchString))! || $0.langVersions?.filter {($0.name?.lowercased().contains(searchString))!}.count != 0 || $0.langVersions?.filter {($0.description?.lowercased().contains(searchString))!}.count != 0 }