1. Entrustment Model 1. Usage process The most common usage of protocols is to pass values through agents, which is the delegation pattern. Common application scenarios include: a view is customized in the controller, and a custom view is added to the view. If some functions or properties in the customized view need to be called in the controller, the delegation pattern is to specify a protocol, let the controller comply with a protocol and provide an implementation, so that the methods in the protocol can be used in the customized view. For example, if you want to add a custom view to a controller, you can click a button in the view to change the value of a label in the controller. The simple code is as follows: Custom views
controller:
This way, you can clearly express your logic. Otherwise, if you want to operate the content of the controller in the view, you need to operate the instance of the controller externally, which causes a problem that you cannot operate the private properties and private methods in the instance (although iOS is a dynamic language and there is no absolute privacy, but who would always use the runtime to operate). 2. Note In ARC, for general delegates, we will specify them as weak in the declaration. When the actual object of this delegate is released, it will be reset to nil. This ensures that even if the delegate no longer exists, we will not crash due to accessing the reclaimed memory. This feature of ARC eliminates a very common crash error in Cocoa development, and it is no exaggeration to say that it has saved thousands of programmers from danger. Of course we would want to do this in Swift. But when we try to write code like this, the compiler won’t let us pass:
Reason: This is because Swift's protocol can be followed by types other than class. Types like struct or enum do not manage memory through reference counting, so it is impossible to modify them with ARC concepts such as weak. Two workarounds:
2. Application of AOP programming ideas First, let us understand the meaning of AOP.
To put it simply, in Swift, protocols are used to cut into certain codes, separating out additional functions without generating coupling, and putting those codes that are not closely related to the main logic together. Common scenarios: logging, performance statistics, security control, transaction processing, exception handling, etc. Continuing with the above example, we need to count once when opening TestViewController and also count when clicking two buttons. The counted content is distinguished by identifer. Let's first create a Statistician.swift to store our statistical logic. (Simulation implementation) Declare a StatisticianProtocal protocol and provide its default implementation.
Next, in any class that needs statistics, we make the class conform to this protocol and call the method in the protocol where needed. If the method that needs to be called in a particular class is slightly different, just rewrite the method in the protocol.
The above code implements the logic of three statistics without writing the statistical logic into the controller file, which reduces the functional coupling. 3. Used to replace extension and enhance code readability Extensions can easily add functions to subclasses that inherit them. This brings up a problem, that is, all subclasses have this method, but the method itself may not be clear, or you only want a few subclasses to use this method. In this case, you can use protocols instead of extensions.
At this time, you can let a subclass comply with the protocol, such as the example above.
If you do not reimplement this method in the class, you can implement the default method. This means that the subclass of the SelectTabbar class complies with the Shakable protocol, which is indirectly equal to SelectTabbar():Shakable?. In this way, we can happily let the SelectTabbar object use this method. (The Self keyword can only be used in protocols or classes, indicating the current class, and can be used as a return value). If you don't want a subclass to use the shakeView() method, it's very simple. Just remove the Shakable protocol in class SelectTabbar: UIView,Shakable. Other practices: By using AOP to separate the data source and event source of the tableview, you can process the logic inside separately, making the tableview's proxy method less redundant. Summarize There are many other uses for protocols. The above are the most common scenarios. If you find that the protocol has better applications in other places during future development, you will update this article. |
<<: Is iOS jailbreaking still your thing? Talking about iOS jailbreaking and its future
>>: This time, I'm not going to stand on Google
As one of the mainstream forms of mobile Internet...
Review expert: Wu Xinsheng, deputy chief physicia...
【Smart Farmers】Traveling around the world: A “foo...
Shi Sanxi's Feng Shui series of lessons: Even...
Reviewer of this article: Xu Qibin, associate chi...
In 1992, when I was in the second grade of elemen...
Course catalog: ├──Video| ├──01 Why is the typeset...
Cold is a general term for a class of diseases wi...
iOS8 opens many APIs, including HomeKit, HealthKi...
□ Popular Science Times reporter Chen Jie and Hu ...
Spring is here, everything is coming back to life...
On September 10, Huawei released a three-fold scr...
Taking Master Kong’s Father’s Day marketing campa...
The scale of my country's online live broadcas...
Infancy: protection period, developing cognitive ...