In swift, how can I define an optional interface like objective-c?

Source: Internet
Author: User

There are @optional keywords in protocol in Objective-c, and the method that is modified by this keyword is not required to be implemented. We can define a series of methods through an interface, and then selectively implement several of these methods by implementing the class of the interface. In many cases the interface method is optional in the Cocoa API, and all methods of protocol in Swift must be implemented completely differently.

 


Methods that do not work properly if they are not implemented are generally necessary, whereas methods that are relatively similar to event notifications or configuration of non-critical properties are generally optional. The best example I think is Uitableviewdatasource and uitableviewdelegate. There are two necessary methods in the former:

Swift Code 
    1. -tableview:numberofrowsinsection:
    2. -tableview:cellforrowatindexpath:


It is used to calculate and prepare the height of the tableview and to provide the style of each cell, while the other methods that return the number of sections or whether the cell can be edited have default behavior, which are optional methods All the methods in the latter (uitableviewdelegate) are detailed configuration and event callbacks, so all are optional.

There are no options available in the native Swift protocol, and all defined methods must be implemented. If we want to define an optional interface method as in objective-c, we need to define the interface itself as OBJECTIVE-C, which is to add @objc before the protocol definition. In addition, unlike @optional in objective-c, we use the keyword optional without the @ symbol to define an optional method:

Swift Code 
    1. @objc protocol Optionalprotocol {
    2. Optional func Optionalmethod ()
    3. }


In addition, for all declarations, their prefix decorations are completely separate. Which means you can't use a @optional like you did in objective-c. Specifies that the next few methods are optional, that each optional method must be prefixed, and that, for methods that do not have a prefix, they must be implemented by default:

Swift Code < param name= "Quality" value= "High" >
    1. @objc protocol Optionalprotocol {
    2. Optional func Optionalmethod ()//optional
    3. Func Necessarymethod ()//Must
    4. Optional func Anotheroptionalmethod ()//optional
    5. }


An unavoidable limitation is that the @objc-modified protocol can only be implemented by class, that is, for struct and enum types, we cannot have an optional method or attribute in the interface that they implement.

In swift, how can I define an optional interface like objective-c?

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.