我想在TableViewController中初始化一个名为TaskList的结构实例,但是我得到了一个"使用未解析的标识符'任务'"错误我使用的每个地方'任务'。当我在类中声明var任务时它工作正常,但是现在它是在另一个.swift文件中声明的var的初始化,我得到了那个错误。我刚刚学习Swift,所以我怀疑这与架构有关,或者搞乱如何从另一个文件中调用对象。有谁知道我需要做些什么来解决它?任何帮助将不胜感激!谢谢大家!
这里是UITableViewController代码:
import UIKit
class LoLFirstTableViewController: UITableViewController {
//var tasks:[Task] = taskData
// Hashed out the above line (even though it worked) because replacing with
// the below line because trying to get instance of TaskList containing all
// properties instead of just the tasks as well as to allow multiple instances
// of TaskList
let exampleList = TaskList(buddy: exampleBuddy, phoneNumber: examplePhoneNumber, tasks: exampleTaskData)
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 60.0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasks.count
}
@IBAction func cancelToLoLFirstTableViewController(_ segue:UIStoryboardSegue) {
}
@IBAction func saveAddTask(_ segue:UIStoryboardSegue) {
if let AddTaskTableViewController = segue.source as? AddTaskTableViewController {
if let task = AddTaskTableViewController.task {
tasks.append(task)
let indexPath = IndexPath(row: tasks.count-1, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
}
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell
let task = tasks[indexPath.row]
cell.task = task
if cell.accessoryView == nil {
let cb = CheckButton()
cb.addTarget(self, action: #selector(buttonTapped(_:forEvent:)), for: .touchUpInside)
cell.accessoryView = cb
}
let cb = cell.accessoryView as! CheckButton
cb.check(tasks[indexPath.row].completed)
return cell
}
func buttonTapped(_ target:UIButton, forEvent event: UIEvent) {
guard let touch = event.allTouches?.first else { return }
let point = touch.location(in: self.tableView)
let indexPath = self.tableView.indexPathForRow(at: point)
var tappedItem = tasks[indexPath!.row] as Task
tappedItem.completed = !tappedItem.completed
tasks[indexPath!.row] = tappedItem
tableView.reloadRows(at: [indexPath!], with: UITableViewRowAnimation.none)
}
}
这里是TaskList结构的代码,如果有帮助:
import UIKit
struct TaskList {
var buddy: String?
var phoneNumber: String?
var tasks: [Task]
init(buddy: String?, phoneNumber: String?, tasks: [Task]) {
self.buddy = buddy
self.phoneNumber = phoneNumber
self.tasks = tasks
}
}
答案 0 :(得分:1)
在你的结构中,tasks数组不是可选的,这意味着你必须传递一个初始化的任务数组。传递初始化数组或将任务数组更改为可选项,就像使用buddy和phoneNumber一样。
import UIKit
struct TaskList {
var buddy: String?
var phoneNumber: String?
// add a question mark to make your array of tasks optional
var tasks: [Task]?
init(buddy: String?, phoneNumber: String?, tasks: [Task]) {
self.buddy = buddy
self.phoneNumber = phoneNumber
self.tasks = tasks
}
}
注意:当您使用struct时,您可以省略它将自动生成的初始值
import UIKit
struct TaskList {
var buddy: String?
var phoneNumber: String?
var tasks: [Task]?
}
现在在你的viewController中初始化你的示例列表
// as your tasks array is optional now even if you pass in a nil it will not crash but it will not have a tasks array
let exampleList = TaskList(buddy: exampleBuddy, phoneNumber: examplePhoneNumber, tasks: exampleTaskData)
记得在使用之前解开你的数组,否则你的应用程序会在任务数组为零时再次崩溃。如果让或保护使用
if let tasks = exampleList.tasks {
// now you can use your tasks array
}