java: constructor cannot be applied to given types
运行时,我收到错误import UIKit
class SearchResultsTableViewController: UITableViewController {
let cocktailController = CocktailController()
var drinks = [Drink]()
var drink: String!
override func viewDidLoad() {
super.viewDidLoad()
title = drink.capitalized
cocktailController.fetchDrinks(forDrink: drink)
{ (drinks) in
if let drinks = drinks {
self.updateUI(with: drinks)
}
}
}
func updateUI(with drinks: [Drink]) {
DispatchQueue.main.async {
self.drinks = drinks
self.tableView.reloadData()
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return drinks.count
}
override func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"DrinkCellIdentifier", for: indexPath)
configure(cell, forItemAt: indexPath)
return cell
}
func configure(_ cell: UITableViewCell, forItemAt indexPath:
IndexPath) {
let drinkItem = drinks[indexPath.row]
cell.textLabel?.text = drinkItem.strDrink
}
}
。程序包中只有一个类,创建实例时的类型似乎是正确的。
编辑:从将来开始,是的;通过阅读文档可以解决这个相当新手的问题。
答案 0 :(得分:1)
构造函数没有没有返回类型。
答案 1 :(得分:1)
此问题的根本原因在于以下代码行:
public class SavingsAccount {
...
// class constructor
public void SavingsAccount(String bankname, String account) {
...
}
}
这不是构造函数。构造函数没有返回类型,该返回类型是通过类名自动推断的。
要解决该问题,只需从void
删除SavingsAccount(...)
关键字即可。
我建议阅读有关构造函数的教程,例如this one by Oracle。