编码中IOS UI中的类和扩展之间的差异
技术是
IOS Swift:编码中IOS UI中的类和扩展之间的差异
注意: 为了便于理解,首先阅读一次此文件 / 按照此自述文件或此视频中的新项目中的步骤进行操作
Class
classes are general purpose , flexible constructs that become the building blocks , define properties and methods to add functionality to classes
import UIKit
class ClassTech: UIButton {
func ClassCreateDesign() {
self.backgroundColor = UIColor.black
self.setTitleColor(UIColor.white, for: .normal)
self.setTitle("test text of button", for: .normal)
}
}
Extension
By extensions can add new functionality to existing types
import UIKit
extension UIButton {
func ExtensionCreateDesign(){
self.backgroundColor = UIColor.black
self.setTitleColor(UIColor.white, for: .normal)
self.setTitle("test text of button", for: .normal)
self.addTarget(self, action: #selector(buttonAction(sender:)), for: .touchUpInside)
}
/// This method in extension UIButton
@objc func buttonAction(sender: UIButton) {
print("Button pushed from Extention")
}
}
Extension can make in Class
extension ClassTech {
func ExtensionFromClassCreateDesign(){
self.backgroundColor = UIColor.black
self.setTitleColor(UIColor.white, for: .normal)
self.setTitle("test text of button", for: .normal)
}
}
Extension can write more than one of the same type
extension ClassTech {
func TestDublicate(){
print("Another Dublicate")
}
}
Thanks
This is inspired by Aya Baghdadi” and copyright for @Technicalisto