Understanding The Usage Of Swift Protocols

Posted by Victoria Brinsley
2
May 10, 2016
197 Views
Image

You must have met Protocols in different interfaces in several other languages. If you're thinking of iPhone app development or planning to hire iPhone App developer then this article will give a clear understanding about Protocols.

These are nothing but a set of behaviors that something should execute, without providing the implementation for those behaviors. The protocol can be confirmed by any class or enumeration with the implementation of methods and variables. The associated type of protocol can ensure the accuracy of the other protocols. They are better used for defining the relationships between abstract protocol. They are little confusing but are required to be adhered to by concrete types.

Protocols use "protocol" keyword. We provide a different type of signatures while defining the protocol. On this event, it mentions the type signatures for any methods that the protocol should implement. Not only this, we specify the property that should be implemented. Both the methods can be utilized well, either "get" and "set" methods or only "get."

1 protocol Animal{

2 var lives:Int { get set }

3 var limbs:Int { get }

4 func makeNoise() -> String

5 }

6

7 class Cat: Animal{

8 var lives = 9

9 var limbs = 4

10

11 func makeNoise() -> String {

12 return "meow"

13 }

14 }

15

16 let cat = Cat() // {lives 9 limbs 4}

17 cat.makeNoise() // "meow"

18 cat.lives // 9

19 cat.lives = 8 // {lives 8 limbs 4}

20 cat.lives // 8

The instance mentioned above explains a protocol called Animal. It indicates to the protocol that it must be implemented to a method makeNoise that returns a string along with two integer properties lives and limbs. It is clear that, for lives, it must be provided both get and set whereas limbs can be read only (only get is required).

The syntax for adopting a protocol to that adapting from another class i.e. a: after the type name followed by the protocol name.

Here in this example, we have mentioned CAT class. It belongs to the animal protocol. To perform this, the cat class provides an implementation to make Noise method and implements integer properties lives and limbs.

The main concern here is to implement a property when conforming to protocol. It should satisfy the requirement i.e. your property can either be stored or computed until get and set are available. Here in this example, both properties are used as stored properties to conform to the protocol.

While specifying the type of method in a protocol, we always look for keyword class, at the same time overlooking the method type for its structure and enumeration.

Other Methods & Properties

At the time of adhering to the protocol, one is expected to perform all the required methods and properties. However, there will be some drawbacks too. Optional properties and methods are demonstrated while using the optional keyword while they are tagged with the @objc keyword1.

Using @objc also imposes additional limitations.

1. @objc protocols can only be used by classes, not by structures or enumerations.

2. Your class must inherit from an Objective-C class

3. If another protocol acquires one protocol, it can only be from one that is using @objc.

4. The protocol is only eligible to use data types that map to Objective-C data types. It ignores arrays and dictionaries.

Here’s an example which has mentioned the optional property.

@objc protocol RunningAnimal{

var limbs:Int { get }

var topSpeed:Int{ get }

optional var lives:Int { get }

}

class Cheetah: NSObject, RunningAnimal{

var limbs = 4

var topSpeed = 120

}

We’ve indicated here that Cheetah class conforms to the RunningAnimal protocol. We've indicated that property is an optional property of the protocol.

Protocol Composition

The variable, property or parameter must be specified that conform to multiple protocols. For instance, in the given below example, the code declares a variable named localizeSecure. It contains the types that conform to both the Localizable and Securable protocols:

var localizeSecure: protocol<localizable, Secureable>

Here in this declaration, Securable protocol can't be stored in this variable or a type that conforms only to the Localizable protocol. The type must respect with both protocols. It is important to note that protocol compositions do not create a new protocol. They only declare a temporary combination of protocols.

Conclusion

Once you've gained knowledge about syntax, it takes very little effort to create a protocol. Overall, the prospective types associated with it are interesting from the safety point of view. They allow designing, complex, interrelated type system with attributes that lead to code stability.

Comments
avatar
Please sign in to add comment.