簡體   English   中英

無法分配值類型

[英]Cannot not assign Value Type

我正在制作一個包含tableView的快捷應用程序,當您單擊該單元格時它會轉到一個URL。 我在tableview內實現了搜索,但是在SearchBar代碼內卻遇到了兩個錯誤。

這是我的代碼。

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