In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "analyzing multi-inheritance and multi-agent in iOS". In the daily operation, I believe that many people have doubts in analyzing the problem of multi-inheritance and multi-agent in iOS. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubts of "analyzing multi-inheritance and multi-agent in iOS". Next, please follow the editor to study!
1. Multiple inheritance
1. Realization process
Classes in swift can abide by multiple protocols, but can only inherit one class, while value types (structures and enumerations) can only comply with single or multiple protocols and cannot do inheritance operations.
Implementation of multiple inheritance: the method of the protocol can be implemented in the extension of the protocol
Protocol Behavior {func run ()} extension Behavior {func run () {print ("Running...")} struct Dog: Behavior {} let myDog = Dog () myDog.run () / / Running...
Both structures, classes and enumerations can abide by multiple protocols, so to achieve multi-inheritance is nothing more than a matter of complying with a few more protocols.
Here is an example.
two。 Extend methods to UIView through multi-inheritance
/ / MARK:-flicker function protocol Blinkable {func blink ()} extension Blinkable where Self: UIView {func blink () {alpha = 1 UIView.animate (withDuration: 0.5, delay: 0.25, options: [.repeat, .autoreverse], animations: {self.alpha = 0})}} / / MARK:-Zoom in and out protocol Scalable {func scale ()} extension Scalable where Self: UIView {func scale () {transform = .identity UIView.animate (withDuration: 0.5, delay: 0.25) Options: [.repeat, .autoreverse], animations: {self.transform = CGAffineTransform (scaleX: 1.5, y: 1.5})}} / / MARK:-add fillet protocol CornersRoundable {func roundCorners ()} extension CornersRoundable where Self: UIView {func roundCorners () {layer.cornerRadius = bounds.width * 0.1 layer.masksToBounds = true}} extension UIView: Scalable, Blinkable, CornersRoundable {} cyanView.blink () cyanView.scale () cyanView.roundCorners ()
In this way, if we customize other View, we only need to zoom in and out and follow the Scalable protocol!
3. Multi-inheritance Diamond problem (Diamond Problem) and its solution
Please look at the following code
Protocol ProtocolA {func method ()} extension ProtocolA {func method () {print ("Method from ProtocolA")}} protocol ProtocolB {func method ()} extension ProtocolB {func method () {print ("Method from ProtocolB")}} class MyClass: ProtocolA, ProtocolB
At this time, both ProtocolA and ProtocolB have a default implementation method method (). Because the compiler does not know which inherited method () method is, it will report an error.
? The diamond problem Diamond Problem occurs when a class or value type has multiple paths in the inheritance graph.
Solution:
1. Override the conflicting method method () in the target value type or class.
two。 Modify the methods repeated in the protocol directly.
At the beginning of the article, we mentioned problem 2, we can try to use multiple agents to solve this problem.
two。 Multiple agents
1. The implementation process of multi-agent
Let's express it in terms of an agent's classic question:
Owners ask pets to eat, eat this action as an agreement, we should achieve unified management.
1. Define the protocol
Protocol MasterOrderDelegate: class {func toEat (_ food: String)}
two。 Define a class: a class used to manage compliance with the protocol
Here, NSHashTable is used to store classes that comply with the protocol. NSHashTable is similar to but different from NSSet. Generally speaking, it has the following characteristics:
1. The elements in NSHashTable can be judged to be equal by Hashable protocol.
2. If the element in NSHashTable is a weak reference, the object will be removed after destruction, which can avoid circular reference.
Class masterOrderDelegateManager: MasterOrderDelegate {private let multiDelegate: NSHashTable = NSHashTable.weakObjects () init (_ delegates: [MasterOrderDelegate]) {delegates.forEach (multiDelegate.add)} / / method in the protocol You can have multiple func toEat (_ food: String) {invoke {$0.toEat (food)}} / / add protocol-compliant classes func add (_ delegate: MasterOrderDelegate) {multiDelegate.add (delegate)} / / delete specified protocol-compliant classes func remove (_ delegateToRemove: MasterOrderDelegate) {invoke {if $0 = = delegateToRemove as AnyObject {multiDelegate.remove ($0)} / delete all protocol-compliant classes func removeAll () {multiDelegate.removeAllObjects ()} / / traverses all protocol-compliant classes private func invoke (_ invocation: (MasterOrderDelegate)-> Void) {for delegate in multiDelegate.allObjects.reversed () {invocation (delegate as! MasterOrderDelegate)}
3. The rest
Class Master {weak var delegate: MasterOrderDelegate? Func orderToEat () {delegate?.toEat ("meat")} class Dog {} extension Dog: MasterOrderDelegate {func toEat (_ food: String) {print ("\ (type (of: self)) is eating\ (food)")} class Cat {} extension Cat: MasterOrderDelegate {func toEat (_ food: String) {print ("\ (type (of: self) is eating\ (food)")} let cat = Cat () let dog = Dog () let cat1 = Cat () The delegate of let master = Master () / / master is a weak reference Therefore, you cannot directly assign let delegate = masterOrderDelegateManager ([cat, dog]) / / add the class delegate.add (cat1) that complies with the protocol / / delete the class delegate.remove (dog) master.delegate = delegatemaster.orderToEat () / / output / / Cat is eating meat// Cat is eating meat
The advantage of setting up masterOrderDelegateManager is that multiple proxies can be managed through an array.
For more information about iOS, please follow my Github: SwiftTips (download locally).
At this point, the study on "analyzing multi-inheritance and multi-agents in iOS" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.