[英]Initializing an instance of struct and getting unresolved identifier error
我想在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
}
}
在您的結構中,tasks數組不是可選的,這意味着您必須傳遞一個初始化的task數組。 傳遞一個初始化數組或將您的任務數組更改為可選數組,就像使用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時,您可以省去Initializer,它會自動生成一個
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)
請記住在使用數組之前先解開數組,否則,如果task數組為nil,則應用可能再次崩潰。 如果讓手或守衛使用
if let tasks = exampleList.tasks {
// now you can use your tasks array
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.