继续创造,加速生长!这是我参加「日新方案 6 月更文应战」的第23天,点击查看活动概况。

在本章中,你将学会Protocol协议的运用办法。

前语

Apple官网或许网上共享的教程类文章不同,我将以实践开发角度叙述Swift言语的一些概念和用法,便利咱们更好地学习和掌握Swift言语。

这一起也是对自己学习Swift言语进程的知识收拾。

如有错误,以你为准。

项目创立

咱们新建一个SwiftUI项目,命名为SwiftUIProtocol

Swift实用小册20: Protocol协议的使用

协议的界说

协议,规定了用来完结某一特定功能所必需的办法和特点。

协议供给具体完结来完结协议界说的办法和功能,能够用在class类、Struct结构体、enum枚举类型上,咱们一般叫做某某“遵从”什么协议

协议的示例

Swift开发进程中,咱们常常会运用到MVC、MVVM开发模式,其间Model部分,常常伴随着协议的界说。示例:

import SwiftUI
struct Model: Identifiable {
var id = UUID()
var title: String
var placehoder: String
}
var Models = [
Model(title: "根底部分", placehoder: "常量和变量的运用"),
Model(title: "根底运算符", placehoder: "根底运算符的运用"),
Model(title: "字符串和字符", placehoder: "字符串的运用"),
Model(title: "调集类型", placehoder: "调集类型的运用"),
Model(title: "操控流", placehoder: "操控流的运用")
]

Swift实用小册20: Protocol协议的使用

上述代码中,咱们创立了一个Model结构体,它遵从Identifiable协议。

Identifiable协议,能够为类或值类型供给稳定的身份概念,这样咱们就能够在View视图中运用由UUID()确定的仅有id的数据。

咱们声明了3个变量:id、title、placehoder,然后创立了一个数组变量Models并赋予了一些示例数据。

因为咱们Model结构体遵从Identifiable协议,因此哪怕咱们数组Models内的数据是彻底相同的,因为Identifiable协议确保在对象的生计期内坚持仅有,它也会被系统界说为2条

咱们在View视图中运用List列表和ForEach循环遍历数组数据,示例:

struct ContentView: View {
var body: some View {
List {
ForEach(Models) { Model in
HStack {
Text(Model.title)
.font(.system(size: 17))
Spacer()
Text(Model.placehoder)
.font(.system(size: 14))
.foregroundColor(Color.gray)
}
}
}
}
}

Swift实用小册20: Protocol协议的使用

协议的创立

上面,咱们演示了Identifiable协议的作用,在Swift开发进程中,咱们运用最多的也是官方供给的协议,像Identifiable协议,Codable协议,Hashable协议。

假如咱们需求创立协议,需求运用到protocol关键字,创立协议的办法和class类、struct结构体创立办法相似。示例:

protocol Person {
func addPerson()
}

上述代码中,咱们创立了一个Person协议,在Person协议内咱们界说了一个办法。

运用Person协议的办法和子类承继父类的办法相同,但是:

承继只能承继一个父类,而类或许结构体能够遵从多个协议。

示例:

struct FullName: Person {
func addPerson() {
print("完结协议办法")
}
}

咱们创立了一个FullName结构体,遵从Person协议,然后在FullName结构体中运用addPerson办法。

协议的特点

协议用于指定特定的实例特点或类特点,而不必指定是存储型特点或核算型特点,此外还必须指明是只读的还是可读可写的。

协议的特点有两种:可读可写特点,只读特点

可读可写特点:{ set get }

只读特点:{ get }

运用协议的特点的办法和声明核算特点或存储特点相似,仅仅后面需求加上关键字。示例:

protocol Person2 {
 var present: Bool{ get }
 var message: String { get set }
}
struct FullName2: Person2 {
var present: Bool = true
var message: String = "称号信息"
}

上述代码中,咱们创立了一个Person2协议,它有2个变量特点:presentmessage,其间present是只读特点,message是可读可写特点。

然后,咱们创立了一个结构体FullName2,遵从Person2协议。咱们就能够直接运用运用Person2协议声明的特点。

Swift实用小册20: Protocol协议的使用

本章代码

import SwiftUI
struct Model: Identifiable {
var id = UUID()
var title: String
var placehoder: String
}
var Models = [
Model(title: "根底部分", placehoder: "常量和变量的运用"),
Model(title: "根底运算符", placehoder: "根底运算符的运用"),
Model(title: "字符串和字符", placehoder: "字符串的运用"),
Model(title: "调集类型", placehoder: "调集类型的运用"),
Model(title: "操控流", placehoder: "操控流的运用")
]
struct ContentView: View {
var body: some View {
List {
ForEach(Models) { Model in
HStack {
Text(Model.title)
.font(.system(size: 17))
Spacer()
Text(Model.placehoder)
.font(.system(size: 14))
.foregroundColor(Color.gray)
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
protocol Person {
func addPerson()
}
struct FullName: Person {
func addPerson() {
print("完结协议办法")
}
}
protocol Person2 {
var present: Bool { get }
var message: String { get set }
}
struct FullName2: Person2 {
var present: Bool = true
var message: String = "称号信息"
}

以上就是本章的全部内容。

快来动手试试吧!

假如本专栏对你有协助,不妨点赞、评论、关注~