I want some of my classes (not all) to conform using 'Class-Only Protocols' from docs. What I am doing is
protocol RefreshData: class, ClassA, ClassB
{
func updateController()
}
and I am getting the errors
non class type 'RefreshData cannot inherit from classA
non class type 'RefreshData cannot inherit from classB
I'm not sure I am following exactly as in the docs. Does anyone have any ideas about this?
Latest version of Swift can do it! I would do a protocol and protocol extensions that target the classes you want! (constraint the extension to specific class)
Swift 4 allows you to combine types, so you can have your protocol and then create, for example, a type alias to combine it with a specific class requirement.
For (a contrived) example:
For the presented code:
The problem is that you're trying to limit to specific classes and Swift can't do that (at the moment anyway). You can only limit to classes and inherit from other protocols. Your syntax is for protocol inheritance but you're trying to use it as a class limitation.
Note that the purpose of class protocols is:
The answers provided by Chris and Wain are correct. I'm just adding a few more details here.
Defining a protocol
You must distinguish the concept of declaring a
protocol
(available for classes)Defining a class
...from the concept of conforming your class to a protocol
Conforming a class you don't own
Sometimes you want to conform a class to a protocol but you don't own the source code for that class. In this case you can use an
extension