- Mastering macOS Programming
- Stuart Grimshaw
- 66字
- 2025-04-04 19:10:25
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.