- Mastering macOS Programming
- Stuart Grimshaw
- 66字
- 2021-07-02 22:54:37
Class-only protocols
If a protocol is to apply to types that are assumed to use reference semantics (rather than being passed by value), then we can create a class protocol that restricts its use to classes:
protocol ClassyProtocol: class
{
func addClassToClassesList() // or whatever
}
Any attempt to declare a value type, whether struct, Int or any other, will be met with a compiler error.