协议(Protocols)为方法,属性和其他需求功能提供了蓝图。 它为方法或属性骨架而不是实现。 通过定义类,函数和枚举,可以进一步完成方法和属性的实现。 协议的一致性满足了协议要求的方法或属性。
语法
协议也遵循与类,结构和枚举类似的语法 -
protocol SomeProtocol {
// protocol definition
}
协议在类,结构或枚举类型名称之后声明。 单个和多个协议声明也是可以的。 如果定义了多个协议,则必须用逗号分隔。
struct SomeStructure: Protocol1, Protocol2 {
// structure definition
}
当要为超类定义协议时,协议名称应使用逗号跟随超类名称。
class SomeClass: SomeSuperclass, Protocol1, Protocol2 {
// class definition
}
属性和方法要求
协议用于指定特定的类类型属性或实例属性。 它只是单独指定类型或实例属性,而不是指定它是存储属性还是计算属性。 此外,它还用于指定属性是gettable
或settable
。
属性需求由var
关键字声明为属性变量。 {get/set}
用于在类型声明后声明gettable
和settable
属性。参考以下一个示例代码 -
protocol classa {
var marks: Int { get set }
var result: Bool { get }
func attendance() -> String
func markssecured() -> String
}
protocol classb: classa {
var present: Bool { get set }
var subject: String { get set }
var stname: String { get set }
}
class classc: classb {
var marks = 96
let result = true
var present = false
var subject = "Swift 4 Protocols"
var stname = "Protocols"
func attendance() -> String {
return "The \(stname) has secured 99% attendance"
}
func markssecured() -> String {
return "\(stname) has scored \(marks)"
}
}
let studdet = classc()
studdet.stname = "Swift 4"
studdet.marks = 98
studdet.markssecured()
print(studdet.marks)
print(studdet.result)
print(studdet.present)
print(studdet.subject)
print(studdet.stname)
当使用playground 运行上述程序时,得到以下结果 -
98
true
false
Swift 4 Protocols
Swift 4
变异方法要求
protocol daysofaweek {
mutating func print()
}
enum days: daysofaweek {
case sun, mon, tue, wed, thurs, fri, sat
mutating func print() {
switch self {
case sun:
self = sun
print("Sunday")
case mon:
self = mon
print("Monday")
case tue:
self = tue
print("Tuesday")
case wed:
self = wed
print("Wednesday")
case mon:
self = thurs
print("Thursday")
case tue:
self = fri
print("Friday")
case sat:
self = sat
print("Saturday")
default:
print("NO Such Day")
}
}
}
var res = days.wed
res.print()
当使用playground 运行上述程序时,得到以下结果 -
Wednesday
初始化器要求
Swing允许用户初始化协议以遵循类似于普通初始化器的类型一致性。
语法
protocol SomeProtocol {
init(someParameter: Int)
}
示例
protocol tcpprotocol {
init(aprot: Int)
}
协议初始化程序要求的类实现
指定或便捷初始化器允许用户初始化协议以使其标准符合保留的required
关键字。
class SomeClass: SomeProtocol {
required init(someParameter: Int) {
// initializer implementation statements
}
}
protocol tcpprotocol {
init(aprot: Int)
}
class tcpClass: tcpprotocol {
required init(aprot: Int) {
}
}
当使用playground 运行上述程序时,得到以下结果 -
Wednesday
通过required
修饰符确保所有子类的协议一致性,用于显式或继承实现。当子类重写其超类初始化要求时,由override
修饰符关键字指定。
protocol tcpprotocol {
init(no1: Int)
}
class mainClass {
var no1: Int // local storage
init(no1: Int) {
self.no1 = no1 // initialization
}
}
class subClass: mainClass, tcpprotocol {
var no2: Int
init(no1: Int, no2 : Int) {
self.no2 = no2
super.init(no1:no1)
}
// Requires only one parameter for convenient method
required override convenience init(no1: Int) {
self.init(no1:no1, no2:0)
}
}
let res = mainClass(no1: 20)
let print = subClass(no1: 30, no2: 50)
print("res is: \(res.no1)")
print("res is: \(print.no1)")
print("res is: \(print.no2)")
当使用playground 运行上述程序时,得到以下结果 -
res is: 20
res is: 30
res is: 50
作为类型的协议
它们不是在协议中实现功能,而是用作函数,类,方法等的类型。协议可以作为以下类型访问 -
- 函数,方法或初始化为参数或返回类型
- 常数,变量或属性
- 数组,字典或其他容器作为项目
示例代码
protocol Generator {
typealias members
func next() -> members?
}
var items = [10,20,30].generate()
while let x = items.next() {
print(x)
}
for lists in map([1,2,3], {i in i*5}) {
print(lists)
}
print([100,200,300])
print(map([1,2,3], {i in i*10}))
当使用playground 运行上述程序时,得到以下结果 -
10
20
30
5
10
15
[100, 200, 300]
[10, 20, 30]
添加协议与扩展的一致性
通过使用扩展,可以采用现有类型并使其符合新协议。 借助扩展,可以将新属性,方法和下标添加到现有类型中。
示例代码
protocol AgeClasificationProtocol {
var age: Int { get }
func agetype() -> String
}
class Person {
let firstname: String
let lastname: String
var age: Int
init(firstname: String, lastname: String) {
self.firstname = firstname
self.lastname = lastname
self.age = 10
}
}
extension Person : AgeClasificationProtocol {
func fullname() -> String {
var c: String
c = firstname + " " + lastname
return c
}
func agetype() -> String {
switch age {
case 0...2:
return "Baby"
case 2...12:
return "Child"
case 13...19:
return "Teenager"
case let x where x > 65:
return "Elderly"
default:
return "Normal"
}
}
}
协议继承
Swift 4允许协议从其定义的属性继承属性。它类似于类继承,但可以选择列出用逗号分隔的多个继承协议。
protocol classa {
var no1: Int { get set }
func calc(sum: Int)
}
protocol result {
func print(target: classa)
}
class student2: result {
func print(target: classa) {
target.calc(sum: 1)
}
}
class classb: result {
func print(target: classa) {
target.calc(sum: 5)
}
}
class student: classa {
var no1: Int = 10
func calc(sum: Int) {
no1 -= sum
print("Student attempted \(sum) times to pass")
if no1 <= 0 {
print("Student is absent for exam")
}
}
}
class Player {
var stmark: result!
init(stmark: result) {
self.stmark = stmark
}
func print(target: classa) {
stmark.print(target: target)
}
}
var marks = Player(stmark: student2())
var marksec = student()
marks.print(target: marksec)
marks.print(target: marksec)
marks.print(target: marksec)
marks.stmark = classb()
marks.print(target: marksec)
marks.print(target: marksec)
marks.print(target: marksec)
当使用playground 运行上述程序时,得到以下结果 -
Student attempted 1 times to pass
Student attempted 1 times to pass
Student attempted 1 times to pass
Student attempted 5 times to pass
Student attempted 5 times to pass
Student is absent for exam
Student attempted 5 times to pass
Student is absent for exam
仅类协议
当定义协议并且用户想要用类定义协议时,应该首先定义类,然后是协议的继承列表来添加协议。
protocol tcpprotocol {
init(no1: Int)
}
class mainClass {
var no1: Int // local storage
init(no1: Int) {
self.no1 = no1 // initialization
}
}
class subClass: mainClass, tcpprotocol {
var no2: Int
init(no1: Int, no2 : Int) {
self.no2 = no2
super.init(no1:no1)
}
// Requires only one parameter for convenient method
required override convenience init(no1: Int) {
self.init(no1:no1, no2:0)
}
}
let res = mainClass(no1: 20)
let print = subClass(no1: 30, no2: 50)
print("res is: \(res.no1)")
print("res is: \(print.no1)")
print("res is: \(print.no2)")
当使用playground 运行上述程序时,得到以下结果 -
res is: 20
res is: 30
res is: 50
协议组成
Swift 4允许使用协议组合一次调用多个协议。
语法
protocol<SomeProtocol, AnotherProtocol>
示例代码
protocol stname {
var name: String { get }
}
protocol stage {
var age: Int { get }
}
struct Person: stname, stage {
var name: String
var age: Int
}
func print(celebrator: stname & stage) {
print("\(celebrator.name) is \(celebrator.age) years old")
}
let studname = Person(name: "Yiibai", age: 21)
print(studname)
let stud = Person(name: "Maxsu", age: 29)
print(stud)
let student = Person(name: "Roshan", age: 19)
print(student)
当使用playground 运行上述程序时,得到以下结果 -
Person(name: "Yiibai", age: 21)
Person(name: "Maxsu", age: 29)
Person(name: "Roshan", age: 19)
检查协议一致性
协议一致性由is
和as
运算符测试,类似于类型转换。
- 如果实例符合协议标准,则
is
运算符返回true
,如果失败则返回false
。 as?
向下转换运算符的版本返回协议类型的可选值,如果实例不符合该协议,则此值为nil
。- 如果向下转换操作符的
as
版本强制转换为协议类型,并且如果向下转换不成功则触发运行时错误。
示例代码
import Foundation
@objc protocol rectangle {
var area: Double { get }
}
@objc class Circle: rectangle {
let pi = 3.1415927
var radius: Double
var area: Double { return pi * radius * radius }
init(radius: Double) { self.radius = radius }
}
@objc class result: rectangle {
var area: Double
init(area: Double) { self.area = area }
}
class sides {
var rectsides: Int
init(rectsides: Int) { self.rectsides = rectsides }
}
let objects: [AnyObject] = [Circle(radius: 2.0),result(area:198),sides(rectsides: 4)]
for object in objects {
if let objectWithArea = object as? rectangle {
print("Area is \(objectWithArea.area)")
} else {
print("Rectangle area is not defined")
}
}
当使用playground 运行上述程序时,得到以下结果 -
Area is 12.5663708
Area is 198.0
Rectangle area is not defined