如何将 .filter 用于具有 Swift 中的结构的数组?
How do I use .filter for an array with structs in Swift?
我正在为我的数学应用设置一个 UISearchBar。搜索栏应该搜索数组 allItems
以查找用户正在搜索的内容。数组 allItems
使用名为 Headline
的结构来定义项目 ID 和项目标题。当我尝试填写 UISearchBar textDidChange 函数时,出现此错误:Cannot assign value of type '[Headline]' to type '[String?]'
我的代码附在下面:
对于数组和结构设置-
struct Headline {
var id: Double
var title: String
}
let allItems = invest+geoArea+geoVolume+geoSA+geoLSA+advPythag+advPhysics+advConv+advOther
构成 allItems
的数组正在使用结构 Headline
对于 UISearchBar-
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchCalc = allItems.filter({[=12=].title.lowercased().prefix(searchText.count) == searchText.lowercased()})
isSearching = true
tableView.reloadData()
}
其他有用的代码:
var searchCalc = [String?]()
isSearching Bool 只是为了帮助应用知道用户何时正在积极搜索某些东西
此外,searchCalc 是用于存储用户当前正在搜索的内容的变量。它应该是字符串,以便应用程序可以根据用户输入的内容过滤掉数组项。
这个代码块会很长,但它可能有助于理解更多。下面是用于在 allItems
-
中创建所有数组的代码
let invest = [
Headline(id: 1.01, title: "Simple Interest"),
Headline(id: 1.02, title: "Compound Interest"),
Headline(id:1.03, title: "Car Loan")
]
let geoArea = [
Headline(id: 2.01, title: "Triangle"),
Headline(id: 2.02, title: "Rectangle"),
Headline(id: 2.03, title: "Parallelogram"),
Headline(id: 2.04, title: "Circle")
]
let geoVolume = [
Headline(id: 2.11, title: "Cube"),
Headline(id: 2.12, title: "Cone"),
Headline(id: 2.13, title: "Cylinder"),
Headline(id: 2.14, title: "Sphere")
]
let geoSA = [
Headline(id: 2.21, title: "Cube"),
Headline(id: 2.22, title: "Rectangular Prism"),
Headline(id: 2.23, title: "Cylinder"),
Headline(id: 2.24, title: "Triangular Prism")
]
let geoLSA = [
Headline(id: 2.31, title: "Rectangular Prism"),
Headline(id: 2.32, title: "Cylinder")
]
let advPythag = [
Headline(id: 3.01, title: "Hypotenuse"),
Headline(id: 3.02, title: "Leg")
]
let advPhysics = [
Headline(id: 3.11, title: "Speed"),
Headline(id: 3.12, title: "Acceleration")
]
let advConv = [
Headline(id: 3.21, title: "Feet to Meters"),
Headline(id: 3.22, title: "Meters to Feet"),
Headline(id: 3.23, title: "Mile to Kilometer"),
Headline(id: 3.24, title: "Kilometer to Mile"),
Headline(id: 3.25, title: "Gallon to Liter"),
Headline(id: 3.26, title: "Liter to Gallon"),
Headline(id: 3.27, title: "Fraction to Decimal"),
Headline(id: 3.28, title: "Feet to Inches"),
Headline(id: 3.29, title: "Inches to Feet"),
Headline(id: 3.210, title: "Fahrenheit to Celsius"),
Headline(id: 3.211, title: "Celsius to Fahrenheit"),
]
let advOther = [
Headline(id: 3.31, title: "Square Root")
]
您的 searchCalc
是 [String?]
类型,因此您不能分配 allItems
数组的子集。您只需从 allItems
数组中获取标题即可解决问题。
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchCalc = [String]()
let matches = allItems.filter({[=10=].title.lowercased().prefix(searchText.count) == searchText.lowercased()})
for match in matches{
searchCalc.append(match.title)
}
isSearching = true
tableView.reloadData()
}
我正在为我的数学应用设置一个 UISearchBar。搜索栏应该搜索数组 allItems
以查找用户正在搜索的内容。数组 allItems
使用名为 Headline
的结构来定义项目 ID 和项目标题。当我尝试填写 UISearchBar textDidChange 函数时,出现此错误:Cannot assign value of type '[Headline]' to type '[String?]'
我的代码附在下面:
对于数组和结构设置-
struct Headline {
var id: Double
var title: String
}
let allItems = invest+geoArea+geoVolume+geoSA+geoLSA+advPythag+advPhysics+advConv+advOther
构成 allItems
的数组正在使用结构 Headline
对于 UISearchBar-
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchCalc = allItems.filter({[=12=].title.lowercased().prefix(searchText.count) == searchText.lowercased()})
isSearching = true
tableView.reloadData()
}
其他有用的代码:
var searchCalc = [String?]()
isSearching Bool 只是为了帮助应用知道用户何时正在积极搜索某些东西
此外,searchCalc 是用于存储用户当前正在搜索的内容的变量。它应该是字符串,以便应用程序可以根据用户输入的内容过滤掉数组项。
这个代码块会很长,但它可能有助于理解更多。下面是用于在 allItems
-
let invest = [
Headline(id: 1.01, title: "Simple Interest"),
Headline(id: 1.02, title: "Compound Interest"),
Headline(id:1.03, title: "Car Loan")
]
let geoArea = [
Headline(id: 2.01, title: "Triangle"),
Headline(id: 2.02, title: "Rectangle"),
Headline(id: 2.03, title: "Parallelogram"),
Headline(id: 2.04, title: "Circle")
]
let geoVolume = [
Headline(id: 2.11, title: "Cube"),
Headline(id: 2.12, title: "Cone"),
Headline(id: 2.13, title: "Cylinder"),
Headline(id: 2.14, title: "Sphere")
]
let geoSA = [
Headline(id: 2.21, title: "Cube"),
Headline(id: 2.22, title: "Rectangular Prism"),
Headline(id: 2.23, title: "Cylinder"),
Headline(id: 2.24, title: "Triangular Prism")
]
let geoLSA = [
Headline(id: 2.31, title: "Rectangular Prism"),
Headline(id: 2.32, title: "Cylinder")
]
let advPythag = [
Headline(id: 3.01, title: "Hypotenuse"),
Headline(id: 3.02, title: "Leg")
]
let advPhysics = [
Headline(id: 3.11, title: "Speed"),
Headline(id: 3.12, title: "Acceleration")
]
let advConv = [
Headline(id: 3.21, title: "Feet to Meters"),
Headline(id: 3.22, title: "Meters to Feet"),
Headline(id: 3.23, title: "Mile to Kilometer"),
Headline(id: 3.24, title: "Kilometer to Mile"),
Headline(id: 3.25, title: "Gallon to Liter"),
Headline(id: 3.26, title: "Liter to Gallon"),
Headline(id: 3.27, title: "Fraction to Decimal"),
Headline(id: 3.28, title: "Feet to Inches"),
Headline(id: 3.29, title: "Inches to Feet"),
Headline(id: 3.210, title: "Fahrenheit to Celsius"),
Headline(id: 3.211, title: "Celsius to Fahrenheit"),
]
let advOther = [
Headline(id: 3.31, title: "Square Root")
]
您的 searchCalc
是 [String?]
类型,因此您不能分配 allItems
数组的子集。您只需从 allItems
数组中获取标题即可解决问题。
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchCalc = [String]()
let matches = allItems.filter({[=10=].title.lowercased().prefix(searchText.count) == searchText.lowercased()})
for match in matches{
searchCalc.append(match.title)
}
isSearching = true
tableView.reloadData()
}