@objc protocol Transaction { func commit() -> Bool optional func isComplete() -> Bool }
The above protocol is marked as the @objc attribute, making it compatible with OBJECTIVE-C code. This is necessary if the Protocol has an optional protocol or attribute, because Swift uses the OBJECTIVE-C runtime to check for the existence of an optional method that the class adheres to.
Unfortunately, this means that an agreement with an alternative method can only be respected by the class, and structs and enumerations cannot adhere to protocols that include optional methods.
The protocol generally does not include optional methods or properties, as they are designed to describe the strict interface of the class. However, optional methods and properties can occasionally become useful, they exist in the cocoa and cocoa touch frameworks, so even if you don't use them in your own code, you may encounter them in some places ;]
The Optional Protocol method is not available until OBJC 2.0. Alternative methods are defined in a method that is used in conjunction with a delegate.
Before OBJC 2.0, delegates are generally defined as ID types, and you need to read their documents to find out how they expect to be implemented. Besides, Classes that use these delegates must check their delegates to see if there is a specific way to implement the wood. There is no way to tell the compiler whether a given method needs to be implemented in a delegate class.
With the introduction of Objc2.0, the delegate class can now use the protocol to define the type. In any case, classes that use delegates often have methods that require and are optional implementations (sometimes only optional methods).
The optional method is a way to enhance and extend the delegate functionality, and OBJC 2.0 adds an optional method to the protocol, allowing the delegate to follow a protocol, so that the programming associated with the delegate becomes a bit easier;]
The historical origins of optional protocols and methods in Swift