我的代码中有两个问题。
首先是let mainframe = coreDataPasswords {
。我得到无法调用非功能类型'[PasswordCoreData]'的值。
第二个位于底部PasswordVC.selectedPassword = selectedholder
一个应该将核心数据数组放置到大型机中,另一个应该选择要单击的密码。
class Home: UITableViewController {
var mainframe : [PasswordCoreData] = []
var selectedPassword : PasswordCoreData?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
getPasswords()
}
func getPasswords() {
if let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext {
if let coreDataPasswords = try? context.fetch(PasswordCoreData.fetchRequest()) as? [PasswordCoreData] {
let mainframe = coreDataPasswords {
tableView.reloadData()
}
}
}
}
func createHolder() -> [Holder] {
let rogers = Holder()
rogers.item = "Rogers"
rogers.username = "username"
rogers.password = "password"
let bell = Holder()
bell.item = "Bell"
bell.username = "username@cibc.com"
bell.password = "password123"
return [rogers, bell]
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mainframe.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
cell.textLabel?.text = mainframe[indexPath.row].item
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "seguePassword", sender: mainframe[indexPath.row].item)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let addVC = segue.destination as? addNewPassword {
addVC.homeVC = self
}
if let passwordVC = segue.destination as? Password {
if let selectedholder = sender as? Password {
passwordVC.selectedPassword = selectedholder
passwordVC.homeVC = self
}
}
}
}
如果需要其他文件,请告诉我。我是编程的新手
答案 0 :(得分:1)
1-您不需要let
和{
if let coreDataPasswords = try? context.fetch(PasswordCoreData.fetchRequest()) as? [PasswordCoreData] {
mainframe = coreDataPasswords
tableView.reloadData()
}
2-确保发件人类型
if let passwordVC = segue.destination as? Password {
if let selectedholder = sender as? Password { ??<< should be type of sent data
因为目标vc与发送的对象相同的转换是不合逻辑的