Protocol in Swift

1. Introduction Protocol

When it comes to Protocol, we're talking about something abstract. So what is abstraction? Normally, we will know "The cat calls meow. The dog calls purring. The duck calls `scooped up ', but I can't say for sure how" pet is going to sound or how to grow up ". So we call it abstraction, although we always know that any animal or bird can be called.

In the programming world, too, people attribute problems / behaviors / attributes that are used in many different types of protocols. To illustrate I will create a CanMakeSound protocol (which can be called) and how to use this protocol.

Here we know that the protocol can be used by class, struct and even enum. Where the protocol is adopted, where (class / struct / enum) must define and write down details for all the methods that the protocol has. Here we see that the protocol looks similar to the concept of interfaces in other object-oriented languages ​​such as Java, C #, … In Swift protocol, there is not only a method but also a property (spreading Episode 2)

2. Mutating methods in the protocol

Mutabling method in protocol means allowing that method to change the intrinsic property (instance property) of Struct or Enum which adopt that protocol. To illustrate I will edit the enum DogStatus above a bit:

3. The init (init) method in the protocol

In Swift we have special methods used to initialize Class, Struct and Enum, without exception (spreading practice 3). The Protocol in Swift can also contain init methods to ensure that anyone who applies it must write details for the initialization function. But for classes, it's a bit different:

For the class, we need the required keyword before the initialization function declaration. I will do another lesson in more detail about the initialization methods.

In fact, we will often encounter this keyword when subclass a UIView and rewrite the init function for it. The catcher must have a required method. Something like this:

The reason is because UIView itself has a 1 protocol named: NSCoding and in this protocol there is a function public init? (ADecoder coder: NSCoder)

4. Protocol for class-only

In short, the class-only protocol means that it can only be used for the class. By default, Protocol is a value type, and we can turn it into a reference type by:

Protocol: class is also used for the purpose of creating delegates. If we use an object (class instance) to be a delegate, we declare with a weak keyword (to retain retain cycle leading to leak memory). But if we assign a value (enum / struct) and this weak variable, we will get an error because weak variable only goes with reference type (class).

5. Optional Protocol Requirements

Now that we know a basic rule when using the protocol, there are many methods in which we have to rewrite many methods when we adopt it. If so, then the protocol will have too many methods, why should I write my tongue too much? @@.

Apple gives us a solution that uses the keyword @objc, which indicates that this protocol can be used with Objective C code, it doesn't sound anything but it really is, regardless of whether we have Whether to use Objective C code in the project. This keyword will also make the protocol become for class-only

So the fact that Protocol in Swift (pure) will not have an optional method … means that if we use Protocol for a struct / enum, how many methods are there to write.

ITZone via IDE Academy

Share the news now