Reprint Annotated Source: http://blog.csdn.net/qxuewei/article/details/53945445
因为OC 的局限性, 使得iOS 开发组件化编程变得不可能,得益于面向对象语言的特性 (封装,继承,多态) 在我们熟悉的设计模式中渐渐形成统一的软件开发思想. 在抽取某些功能作为基类的不断运用中,代码的可移植性逐渐减弱. 就如同一棵树,从主干到各个分支,每个分支再长成细枝末叶.代码的耦合性也相应增加. 随着苹果 swift 语言的推出, 对于传统OC 语言取其精华,弃其糟粕. 加上开源社区众大神的齐力维护.逐渐成为一门非常优秀的高级语言. 其中 swift中的面向协议编程思想 就是其中很有代表性的一项优秀组成部分.
The methods defined in the agreement in OC must be implemented in the inherited class, which weakens the functionality of the Protocol.
The collaboration of the main class and the extension extension class in Swift preserves the attributes that define methods in the main class to be implemented in the inherited classes, and new methods that are defined and implemented in the extension extension class can be called arbitrarily in any class that inherits from this protocol. For modular programming
As with the creation of a car, any model under the brand can directly call the brand production of various components, in fact, maximize the benefits.
Two simple usage scenarios can fully reflect the benefits of Swift's modular programming:
1.xib Load UIView
我们使用XIB加载的自定义View都会使用相同的方法:
Bundle.main.loadNibNamed("RedView", owner: nil, options: nil)?.last
A class method is typically defined in the implementation of a custom view
For example:
class func loadWithNib() -> RedView { returnBundle.main.loadNibNamed("RedView"owner:niloptions:nilRedView }
If we define a protocol that can load a class from Xib, all the methods implemented in this protocol can be implemented in a custom class by simply adhering to the Protocol, without implementing the methods in the protocol. For example, define a protocol that can be loaded from xibNibLoadable
import Foundationprotocol NibLoadable {}extension NibLoadable { staticSelf { return Bundle.main.loadNibNamed("\(self)"asSelf }}
As long as this protocol is inherited in Xib-loaded classes
import UIKitclass GreenView: UIView , NibLoadable{}
You can call directly when you need to initialize this object.
GreenView.loadViewWithNib()
2. Controller with particle effect
Define a protocol that can add and remove particle effects, and any controller that adheres to this Protocol (note that the protocol indicates that where Self : UIViewController only the controller class can comply with this protocol). Invoke the method implemented in the protocol directly in the controller that requires the particle effect:
Protocol particleable to implement particle animation
Particleable. Swiftxwprotocoldemo////Created by Chu Cowei on ./1/3.Copyright? .Year Chu Cowei. All rights reserved.//can implement the import of particle animation Foundationimport uikitprotocol particleable {}extension particleable where self:u Iviewcontroller {///Add particle effect position particle emitter position Func addparticle (_ Position:cgpoint = Cgpoint (x: UIScreen. Main. Bounds. Width*0.85,y: UIScreen. Main. Bounds. Height- -) {Let emitter = Caemitterlayer () emitter. Position= Position Emitter. Preservesdepth= true var cells = [Caemittercell] () for Iinch 0..<Ten{Let cell = Caemittercell () cell. birthrate= Float (Arc4random_uniform (4)) +3Cell. Lifetime=5Cell. Lifetimerange=3Cell. scale=0.7Cell. Scalerange=0.3Cell. Emissionlongitude= CGFloat (-m_pi_2) cell. Emissionrange= CGFloat (M_pi_4 *0.6) cell. Velocity= -Cell. Velocityrange= -Cell. Spin= CGFloat (m_pi_2) cell. Contents= UIImage (named:"good\ (i) _30x30")?. CgimageCells. Append(cell)} Emitter. Emittercells= Cells View. Layer. Addsublayer(Emitter)} Delete Particle effect func removeparticle () {view. Layer. Sublayers?. Filter({ $0.Iskind (Of:caemitterlayer. Self) }). Last?. Removefromsuperlayer() }}
Complete Demo:https://github.com/qxuewei/xwcsdndemos in Xwprotocoldemo project
Ios-swift-oriented protocol programming/component-based (modular) programming ideas