Optional function in swift Protocol

In this article we will check how to make the protocol functions optional for implementation.

Let’s create a protocol named Eatable and make our VegetableVC class conform to this.

protocol Eatable{
func addItem()
}

class VegetableVC:Eatable{


This content originally appeared on DEV Community and was authored by NalineeR

In this article we will check how to make the protocol functions optional for implementation.

Let's create a protocol named Eatable and make our VegetableVC class conform to this.

protocol Eatable{
    func addItem()
}

class VegetableVC:Eatable{

}

You will get an error if you run with this code. Because the functions in protocol are of Required type by default i.e. whoever conforms to this will have to implement this.

Image description

So let's add the functions to remove this error.

class VegetableVC:Eatable{
    func add() {

    }
}

But what if we want to add a function which is optional to implement? Let's check how to do it -

First lets add a new function as follow - func delete()

Now we have 2 approaches to achieve the optional implementation -

1. using extension - In this we extend the protocol and provide a default implementation.

extension Eatable{
    func delete(){

    }
}

If you run the code now you see you don't get error even though VegetableVC class has not implemented the delete() method.

2. using optional keyword - In this approach we mark the function as optional using the keyword.

@objc protocol Eatable{
    func add()
    @objc optional func delete()
}

You can see we have marked protocol and func with @objc becuase 'optional' can only be applied to members of an @objc protocol.

Delete the extension we added previously and run the program.
It runs without any error.


This content originally appeared on DEV Community and was authored by NalineeR


Print Share Comment Cite Upload Translate Updates
APA

NalineeR | Sciencx (2023-04-13T09:51:48+00:00) Optional function in swift Protocol. Retrieved from https://www.scien.cx/2023/04/13/optional-function-in-swift-protocol/

MLA
" » Optional function in swift Protocol." NalineeR | Sciencx - Thursday April 13, 2023, https://www.scien.cx/2023/04/13/optional-function-in-swift-protocol/
HARVARD
NalineeR | Sciencx Thursday April 13, 2023 » Optional function in swift Protocol., viewed ,<https://www.scien.cx/2023/04/13/optional-function-in-swift-protocol/>
VANCOUVER
NalineeR | Sciencx - » Optional function in swift Protocol. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/04/13/optional-function-in-swift-protocol/
CHICAGO
" » Optional function in swift Protocol." NalineeR | Sciencx - Accessed . https://www.scien.cx/2023/04/13/optional-function-in-swift-protocol/
IEEE
" » Optional function in swift Protocol." NalineeR | Sciencx [Online]. Available: https://www.scien.cx/2023/04/13/optional-function-in-swift-protocol/. [Accessed: ]
rf:citation
» Optional function in swift Protocol | NalineeR | Sciencx | https://www.scien.cx/2023/04/13/optional-function-in-swift-protocol/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.