Ios 不能指定值类型
我正在制作一个swift应用程序,它包含一个tableView,当你点击单元格时,它会转到一个url。我在tableview中实现了搜索,但在搜索条形码中出现了两个错误 这是我的密码Ios 不能指定值类型,ios,swift,uitableview,uisearchbar,uisearchbardelegate,Ios,Swift,Uitableview,Uisearchbar,Uisearchbardelegate,我正在制作一个swift应用程序,它包含一个tableView,当你点击单元格时,它会转到一个url。我在tableview中实现了搜索,但在搜索条形码中出现了两个错误 这是我的密码 import Foundation import UIKit class ViewControllerAnalysis: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate { @IBOutl
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()
}
}
Filteredata的类型应为[TableItem],与数据相同。我不是Swift开发人员,因此我不确定是否应将其标记为重复,但请注意:顺便说一句,如果您在上面搜索错误代码,许多问题将弹出许多答案。如果你仔细看看,也许其中一个可以提前解决你的问题。你能说得更具体一点吗?这是解决两个错误还是只解决一个错误@BistaSo先生,我应该编辑哪行代码来解决错误@BistaChange先生
var filteredata:[字符串]代码>至var filteredData:[TableItem]代码>我实现了这个,错误消失了,但当我将文本放入搜索栏时,它没有过滤。你知道它为什么这么做吗@Mr.BistaUse Filteredata位于函数编号行部分,cellForRowAt@卢克