How to define optional methods in Swift protocol?
问题 Is it possible in Swift? If not then is there a workaround to do it? 回答1: 1. Using default implementations (preferred). protocol MyProtocol { func doSomething() } extension MyProtocol { func doSomething() { /* return a default value or just leave empty */ } } struct MyStruct: MyProtocol { /* no compile error */ } Advantages No Objective-C runtime is involved (well, no explicitly at least). This means you can conform structs, enums and non- NSObject classes to it. Also, this means you can take