About Swift protocol compliance (for example, unknown classes such as Java)

Asked 2 years ago, Updated 2 years ago, 118 views

Hello
I'm sorry to ask you something I can't do, but I'm having trouble because I don't know how to implement the protocol.
I would appreciate it if someone could reply.

For reasons such as containerView, one ViewController has multiple ViewControllers in its properties (array called viewControllers) and I will make sure that each ViewController has one tableView.

override func setEditing(_editing:Bool, animated:Bool){
    super.setEditing (editing, animated: animated)
    iflet firstVC = viewControllers [currentIndex] as ? FirstViewController {
        firstVC.tableView.isEditing=editing
    }
    iflet secondVC = viewControllers [currentIndex] as ? SecondViewController {
        secondVC.tableView.isEditing=editing
    }
    iflet thirdVC = viewControllers [currentIndex] as ? ThirdViewController {
        thirdVC.tableView.isEditing=editing
    }
}

It's good to downcast each one like this, but I think it's a bit monotonous.

I would like to have a UIViewController class that has tableView as the parent of ViewControllers in the array.
How do I implement it in Swift language using protocols...?

swift swift3

2022-09-30 21:21

1 Answers

I'm not sure if it's a good feeling or not, but if I only take the part that says, "Use protocol in Swift language," it'll look like this.

protocolHavingTableView{
    variableView:UITableView!{get}
}
extension FirstViewController:HavingTableView{}
extensionSecondViewController:HavingTableView{}
extensionThirdViewController:HavingTableView{}

//...
    override func setEditing(_editing:Bool, animated:Bool) {
        super.setEditing (editing, animated: animated)
        iflettableViewVC = viewControllers [currentIndex] as ? US>HavingTableView {
            tableViewVC.tableView.isEditing=editing
        }
    }

The current Swift cannot combine classes and protocols to create a data type like UIViewController&HavingTableView, so the viewControllers are assumed to be [UIViewController]Then as? You may need a cast like HavingTableView, and it may not look very "good."

If you say, "I want a UIViewController class that has tableView as the parent of ViewControllers in an array," you can define it as a common parent class.

class MyBaseViewController:UIViewController {
    variableView —UITableView!
}
class FirstViewController:MyBaseViewController {
    //...
}
classSecondViewController:MyBaseViewController {
    //...
}
classThirdViewController:MyBaseViewController {
    //...
}

//...        
    varviewControllers: MyBaseViewController = [ ]

    override func setEditing(_editing:Bool, animated:Bool) {
        super.setEditing (editing, animated: animated)
        viewControllers [currentIndex].tableView.isEditing=editing
    }

I personally think this is better, but if you want tableView to be @IBOutlet, it might be a bit troublesome.


2022-09-30 21:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.