-1

我正在为我的数学应用程序设置一个 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({$0.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")
]
4

1 回答 1

0

searchCalc是 type [String?],因此您不能分配allItems数组的子集。您可以通过从allItems数组中获取标题来解决问题。

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    searchCalc = [String]()
    let matches = allItems.filter({$0.title.lowercased().prefix(searchText.count) == searchText.lowercased()})
    for match in matches{
        searchCalc.append(match.title)
    } 
    isSearching = true
    tableView.reloadData()
}
于 2019-07-27T00:15:25.273 回答