【问题标题】:Initializing an instance of struct and getting unresolved identifier error初始化结构实例并获得未解决的标识符错误
【发布时间】:2017-05-17 13:09:32
【问题描述】:

我想在我的 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
    }
}

【问题讨论】:

    标签: ios uitableview struct initialization


    【解决方案1】:

    在您的结构中,任务数组不是可选的,这意味着您必须传递一个初始化的任务数组。传递一个初始化的数组或将你的任务数组更改为可选的,就像你对 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
      }
    }
    

    注意:当你使用结构体时,你可以省略初始化器,它会自动生成一个

    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)
    

    记得在使用之前解开你的数组,否则如果 tasks 数组为 nil,你的应用程序可能会再次崩溃。使用 if let 或 guard

    if let tasks = exampleList.tasks {
       // now you can use your tasks array
    }
    

    【讨论】:

    • 如果我在 viewDidLoad 中解开数组,“使用未解析的标识符 'tasks'”仍然保留在所有这些地方(因为 'tasks' 仅在 viewDidLoad 之外的其他函数下使用)。如果我在 cellForRowAt indexPath 中展开它,则在其他函数中每次使用“任务”时都会出现错误。我应该在哪里打开它以实现所有功能,还是我真的需要在每个功能下打开它?谢谢,哈立德,感谢您的帮助!我是这方面的新手。
    • 可选的规则是总是先解包然后使用它。除非您 1000% 确定,否则您可以强制打开它。否则,您必须在要访问它的每个方法中使用 if let 。此外,您必须确保在任务中传递一些数据是
    • 在任务中传递一些数据是什么?你这是什么意思?
    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 2018-01-20
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    相关资源
    最近更新 更多