The historical origins of optional protocols and methods in Swift

Source: Internet
Author: User

@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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.