【问题标题】:Cannot not assign Value Type不能分配值类型
【发布时间】:2017-02-09 01:21:42
【问题描述】:

我正在制作一个快速应用程序,其中包含一个 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()
    }
}

【问题讨论】:

  • 我不是 Swift 开发人员,所以我不确定是否应该将其标记为重复,但在这里:stackoverflow.com/questions/31161381/…btw,如果您在 SO 上搜索错误代码,很多 问题会弹出很多答案。如果您仔细查看它们,也许其中之一可以提前解决您的问题。

标签: ios swift uitableview uisearchbar uisearchbardelegate


【解决方案1】:

filteredData的类型应该是[TableItem],和data一样。

【讨论】:

  • 你能再具体一点吗?这是否解决了两个错误或仅解决了一个错误? @Mr.Bista
  • 那么我应该具体编辑哪一行代码来解决错误? @Mr.Bista
  • var filteredData: [String]! 更改为var filteredData: [TableItem]!
  • 我实现了这个并且错误消失了,但是当我将文本放入 SearchBar 时它没有过滤。对它为什么这样做有任何想法吗? @Mr.Bista
  • 在函数 numberOfRowsInSection、cellForRowAt 中使用过滤数据。 @Luc
猜你喜欢
  • 2021-07-02
  • 2021-07-23
  • 2021-08-22
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多