协议规定了用来实现某一特定功能所必需的方法和属性。
语法
1 2 3
| protocol SomeProtocol { }
|
要使类遵循某个协议,需要在类型名称后加上协议名称,中间以冒号:分隔,作为类型定义的一部分。遵循多个协议时,各协议之间用逗号,分隔。
1 2 3
| struct SomeStructure: FirstProtocol, AnotherProtocol { }
|
如果类在遵循协议的同时拥有父类,应该将父类名放在协议名之前,以逗号分隔。
1 2 3
| class SomeClass: SomeSuperClass, FirstProtocol, AnotherProtocol { }
|
对属性的规定
- 协议用于指定特定的实例属性或类属性,而不用指定是存储型属性或计算型属性。此外还必须指明是只读的还是可读可写的。
- 协议中的通常用var来声明变量属性,在类型声明后加上{ set get }来表示属性是可读可写的,只读属性则用{ get }来表示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| 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 协议" 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" studdet.marks = 98 studdet.markssecured() print(studdet.marks) print(studdet.result) print(studdet.present) print(studdet.subject) print(studdet.stname)
|
对 Mutating 方法的规定
值类型(结构体,枚举)的实例方法中,将mutating关键字作为函数的前缀,写在func之前,表示可以在该方法中修改它所属的实例及其实例属性的值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| protocol daysofaweek { mutating func show() } enum days: daysofaweek { case sun, mon, tue, wed, thurs, fri, sat mutating func show() { 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 .thurs: self = .thurs print("Wednesday") case .fri: self = .fri print("Wednesday") case .sat: self = .sat print("Saturday") default: print("NO Such Day") } } } var res = days.wed res.show()
|
协议构造器规定在类中的实现
你可以在遵循该协议的类中实现构造器,并指定其为类的指定构造器或者便利构造器。在这两种情况下,你都必须给构造器实现标上”required”修饰符:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class SomeClass: SomeProtocol { required init(someParameter: Int) { } }
protocol tcpprotocol { init(aprot: Int) }
class tcpClass: tcpprotocol { required init(aprot: Int) { } }
|
使用required修饰符可以保证:所有的遵循该协议的子类,同样能为构造器规定提供一个显式的实现或继承实现。
如果一个子类重写了父类的指定构造器,并且该构造器遵循了某个协议的规定,那么该构造器的实现需要被同时标示required和override修饰符:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| protocol tcpprotocol { init(no1: Int) }
class mainClass { var no1: Int init(no1: Int) { self.no1 = no1 } }
class subClass: mainClass, tcpprotocol { var no2: Int init(no1: Int, no2 : Int) { self.no2 = no2 super.init(no1:no1) } required override convenience init(no1: Int) { self.init(no1:no1, no2:0) } } let res = mainClass(no1: 20) let show = subClass(no1: 30, no2: 50)
print("res is: \(res.no1)") print("res is: \(show.no1)") print("res is: \(show.no2)")
|
协议类型
尽管协议本身并不实现任何功能,但是协议可以被当做类型来使用。
协议可以像其他普通类型一样使用,使用场景:
- 作为函数、方法或构造器中的参数类型或返回值类型
- 作为常量、变量或属性的类型
- 作为数组、字典或其他容器中的元素类型
在扩展中添加协议成员
我们可以可以通过扩展来扩充已存在类型( 类,结构体,枚举等)。
扩展可以为已存在的类型添加属性,方法,下标脚本,协议等成员。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| 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" } } }
|
协议的继承
协议能够继承一个或多个其他协议,可以在继承的协议基础上增加新的内容要求。
1 2 3
| protocol InheritingProtocol: SomeProtocol, AnotherProtocol { }
|
类专属协议
1 2 3
| protocol SomeClassOnlyProtocol: class, SomeInheritedProtocol { }
|
协议合成
Swift 支持合成多个协议,这在我们需要同时遵循多个协议时非常有用。
检验协议的一致性
你可以使用is和as操作符来检查是否遵循某一协议或强制转化为某一类型。
- is操作符用来检查实例是否遵循了某个协议。
- as?返回一个可选值,当实例遵循协议时,返回该协议类型;否则返回nil。
- as用以强制向下转型,如果强转失败,会引起运行时错误。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| protocol HasArea { var area: Double { get } }
class Circle: HasArea { let pi = 3.1415927 var radius: Double var area: Double { return pi * radius * radius } init(radius: Double) { self.radius = radius } }
class Country: HasArea { var area: Double init(area: Double) { self.area = area } }
class Animal { var legs: Int init(legs: Int) { self.legs = legs } }
let objects: [AnyObject] = [ Circle(radius: 2.0), Country(area: 243_610), Animal(legs: 4) ]
for object in objects { if let objectWithArea = object as? HasArea { print("面积为 \(objectWithArea.area)") } else { print("没有面积") } }
|