This is the second day of my participation in Gwen Challenge
Protocol oriented Programming (POP -)
A protocol describes the properties and methods that a thing must have; You can tell the Swift class to comply with this protocol
For example, let’s write a function that takes an ID attribute. But you don’t need to care about this data type; So we create an Identifiable agreement, and all the other objects that contrarily obey this need to have an ID String, and we need to have both ‘get’ and ‘set’
protocol Identifiable {
var id: String { get set}}Copy the code
struct User: Identifiable {
var id: String
}
Copy the code
func displayID(thing: Identifiable) {
print("My ID is \(thing.id)")}Copy the code
Agreement inheritance
Protocols can inherit from another defined protocol. Unlike classes, you can inherit from more than one protocol at a time
protocol Payable {
func calculateWages(a) -> Int
}
protocol NeedsTraining {
func study(a)
}
protocol HasVacation {
func takeVacation(days: Int)
}
Copy the code
Create an Employee with multiple protocols defined above, split;
protocol Employee: Payable.NeedsTraining.HasVacation {}Copy the code
Extensions extension
- Extensions in Swift are similar to classification categories in OC
- Extensions can add new functionality to enumerations, structs, classes, and protocols
Extensions allow us to add extension methods to existing types, adding a method that does not already exist;
OC is allowed to override, Swift is secure and is not allowed to override
extension Int {
func squared(a) -> Int {
return self * self}}let number = 8
number.squared()
Copy the code
Swift cannot add storage attributes to an extension; You can use computed properties instead
Storage properties change the structure of memory, but extensions do not allow that to happen
extension Int {
var isEven: Bool {
return self % 2 = = 0}}Copy the code
Protocol Oriented Programming (POP)
The Protocol extension can provide a default implementation for the Protocol methods we define
For example, we have a protocol called Identifiable, which requires us to have an ID property and identify() method
protocol Identifiable {
var id: String { get set }
func identify(a)
}
Copy the code
Provide a default implementation
extension Identifiable {
func identify(a) {
print("My ID is \(id).")}}Copy the code
As long as the User object complies with the Identifiable agreement, it can automatically call the identify () function
struct User: Identifiable {
var id: String
}
let twostraws = User(id: "twostraws")
twostraws.identify()
Copy the code
Protocol and Delegate
A Delegate is a design pattern that represents delegating part of the functionality of one object to another. The delegate pattern can be used to respond to specific actions, or to receive data from an external data source, regardless of the type of the external data source. In some cases, delegates are more loosely coupled than top-down inheritance, effectively reducing code complexity.
So what’s the relationship between Deleagte and Protocol? In Swift, a Delegate is implemented based on Protocol, which is defined to encapsulate functions that need to be delegated, ensuring that the type of Protocol that follows provides those functions.
Protocol is one of Swift’s language features, and Delegate uses Protocol for decoupling purposes.
protocol CustomButtonDelegate: AnyObject{
func CustomButtonDidClick(a)
}
class ACustomButton: UIView {
weak var delegate: CustomButtonDelegate?
func didClick(a) {
delegate?.CustomButtonDidClick()}}// Follow the delegate class
class ViewController: UIViewController.CustomButtonDelegate {
let buttonView:ACustomButton = ACustomButton(a)override
func viewDidLoad(a) {
super.viewDidLoad()
buttonView.delegate = self
}
func CustomButtonDidClick(a) {
print("Delegation works!")}}Copy the code
conclusion
- An agreement defines a contract that must be followed
- Can follow a number of protocols, access to power methods; Calss cannot inherit more
- The protocol extension Extensions can provide a default implementation
- Protocol oriented programming pop
- The relationship between agreement and agent