简体   繁体   English

无法分配值类型

[英]Cannot not assign Value Type

I am making a swift app that involves a tableView that goes to a url when you click the cell. 我正在制作一个包含tableView的快捷应用程序,当您单击该单元格时它会转到一个URL。 I implemented search inside of the tableview but I am getting two errors inside of the SearchBar code. 我在tableview内实现了搜索,但是在SearchBar代码内却遇到了两个错误。

Here is my code. 这是我的代码。

import Foundation
import UIKit

class ViewControllerAnalysis: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

    @IBOutlet weak var searchBar: UISearchBar!

    @IBOutlet weak var tableview: UITableView!

    struct TableItem {
        let url: String
        let name: String
        let symbol: String
    }

    let data = [
        TableItem(
            url: "https://www.zacks.com/stock/quote/AAPL?q=aapl?q=AAPL?q=AAPL",
            name: "Apple Inc.",
            symbol: "AAPL"
        ),
        TableItem(
            url: "https://www.zacks.com/stock/quote/GOOG?q=goog?q=GOOG?q=GOOG",
            name: "Google Inc.",
            symbol: "GOOG"
        ),
        TableItem(
            url: "https://www.zacks.com/stock/quote/FB?q=fb?q=FB?q=FB",
            name: "Facebook Inc.",
            symbol: "FB"
        ),
        TableItem(
            url: "https://www.zacks.com/stock/quote/AMZN?q=amzn?q=AMZN?q=AMZN",
            name: "Amazon",
            symbol: "AMZN"
        )
    ]

    let cellIdentifier = "Cell"

    var filteredData: [String]!

    override func viewDidLoad() {
        super.viewDidLoad()

        var filteredData = data

        self.tableview.dataSource = self
        self.tableview.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.data.count
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! StockTableViewCell

        let item = data[indexPath.row]cell.stockNameLabel?.text = item.name
        cell.stockSymbolLabel?.text = item.symbol

        return cell
    }



    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = data[indexPath.row]
        let url = URL(string: item.url)
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url!, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url!)
        }
    }

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchText.isEmpty {
            filteredData = data
            Here is where I am getting "Cannot Assign Value Type"^^^
            On the filteredData = Data
        } else {
            filteredData = data.filter { $0.name.range(of: searchText, options: .caseInsensitive) != nil }
            Then right here I am getting "Cannot invoke 'filter' with an argument list of type."^^^
        }
        tableview.reloadData()
    }

    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        searchBar.showsCancelButton = true

    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchBar.showsCancelButton = false
        searchBar.text = ""
        searchBar.resignFirstResponder()
    }
}

filterData的类型应为[TableItem],与数据相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM