Error Use of undeclared type , when trying to use the identifier of one screen to carry information, to another

Asked

Viewed 121 times

0

I am studying Swift and at the moment I am trying to make an application with more than one screen, very simple.

But I’m having trouble working with these inter-screen, storyboard connections.

I identified one of the components of one of the screens as ToDoItem, image below:

inserir a descrição da imagem aqui

I’m trying to click the line and carry the value to another screen, but I can’t do the cast for the identifier ToDoItem, Xcode always returns an error to me:

Follow the code of Todolist:

class ToDoListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet weak var tableView: UITableView!
    var toDoItems: [ToDoItemModel] = [ToDoItemModel]()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView()
        title = "To Do List"
        
        let testItem = ToDoItemModel(name: "Test Item", details: "TestDetails", completionDate: Date())
        self.toDoItems.append(testItem)
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let selectedItem = toDoItems[indexPath.row]
        
        performSegue(withIdentifier: "TaskDetailsSegue", sender: selectedItem ) // -> passa dados para outra tela
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return toDoItems.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let toDoItem = toDoItems[indexPath.row]
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoItem")!
        
        cell.textLabel?.text = toDoItem.isComplete ? "Complete" : "Incomplete"
        
        return cell
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        if segue.identifier == "TaskDetailsSegue"{
            
            guard let destinationVC = segue.destination as? ToDoDetailsViewController else { return }
            
            guard let toDoItem = sender as? ToDoItem else { return } //erro
            
            destinationVC.toDOItem = toDoItem
            
        }
        
    }

}

The mistake Use of undeclared type 'ToDoItem' appears on the Guard line let toDoItem = sender as? ToDoItem else { return }

What should I do to get this information and take it to another screen of this storyboard?

1 answer

1


This error indicates that he doesn’t know the type ToDoItem.

You are passing on the following Sender:

        let selectedItem = toDoItems[indexPath.row]
        
        performSegue(withIdentifier: "TaskDetailsSegue", sender: selectedItem )

That is, Sender is an element of the toDoItens array, which in turn is of the type:

var toDoItems: [ToDoItemModel]

therefore, each element is a ToDoItemModel.

Replace the line with the following:

guard let toDoItem = sender as? ToDoItemModel else { return } //erro

and this should bring the expected result.

  • 1

    I got it, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.