1.声明protocol
protocol myFirstProtocol{
//properties and methods declarations go in here.
}
2.引用协议
结构体、类、枚举都可以实现协议
class A : myFirstProtocol{
//enter your code
}
struct B : myFirstProtocol{
//enter your code
}
enum E : myFirstProtocol{
//enter your code
}
3.协议说明
1、协议可以包含签订协议的类、结构体、枚举需要实现的属性和方法
2、协议中的属性必须是可变的
3、协议中的属性必须用var修饰
4、我们必须明确说明属性类型({get} {get set}),如下:
protocol FirstProtocol{
var age : Int {get set} //read-write property
var year : Int {get} //optional write property
}
5.{get set}代表属性可以读写,{get}代表属性只读;如果用户确认当前属性是只读的,在实现的时候可以用let接受这个属性值.
class ClassA : FirstProtocol{
var age = 23
var year = 2017
}
var a = ClassA()
a.year = 2018
print(a.year)
class ClassB : FirstProtocol{
var age = 23
let year = 2017 //now this is a constant.
}
6、协议中的函数是在没有主体的情况下定义的
7、函数中不允许使用默认值
8、函数中可以使用多变参数
9、通过添加关键字Mutating,可以在关键字内部定义Mutating函数
10、类实现mutating函数时,可以不用写mutating关键字
11、mutating只用于结构体和枚举实现协议时,然后在函数内部可以更改结构体和枚举内的变量值,不然改变不了
列子:
protocol CSDegree{
var cgpa : Double {get set}
var name : String? {get set}
var coursesCompleted : Bool? {get set}
func haveYouCompletedOperatingSystems() -> Bool
func haveYouCompletedJavaCourse() -> Bool
func printAllGrades(grade : String...)
func degreeGranted()
mutating func hackToGetMyDegree()
}
class Student : CSDegree{
var cgpa : Double = 2
var coursesCompleted : Bool?
var name : String?
func haveYouCompletedJavaCourse() -> Bool{
return coursesCompleted ?? false
}
func haveYouCompletedOperatingSystems() -> Bool{
return coursesCompleted ?? false
}
func printAllGrades(grade : String...){
if let n = name{
print("Grades of (n) in all the courses are (grade)")
}
else{
print("Student not found")
}
}
func degreeGranted() {
if haveYouCompletedJavaCourse() && haveYouCompletedOperatingSystems() && cgpa > 4
{
print("Congrats, you've completed Bachelors in Computer Science")
}
else{
print("Sorry, you won't be granted your degree this year")
}
}
func hackToGetMyDegree() {
cgpa = 8.5
coursesCompleted = true
}
}
var s = Student()
s.cgpa = 6.93
s.name = "Anupam"
s.coursesCompleted = false
print(s.name ?? "Not found") //prints "Anupam
"
s.haveYouCompletedOperatingSystems()
s.haveYouCompletedJavaCourse()
s.degreeGranted() //Sorry, you won't be granted your degree this year
s.printAllGrades(grade: "A","D") //prints Grades of Anupam in all the courses are ["A", "D"]
//这个时候我们可以调用可变函数,修改协议内部参数值
s.hackToGetMyDegree()
s.degreeGranted()
//打印结果:Congrats, you've completed Bachelors in Computer Science
协议中可以定义便利构造器,然后可以作为实现协议的类、结构体、枚举的初始化便利器,当用required修饰与convenience修饰后。
required:
protocol MyInitialiser {
init(name: String)
}
class TestInitProtocol : MyInitialiser{
required init(name: String) {
print("Welcome to (name)")
}
}
var t = TestInitProtocol(name: "JournalDev") //prints Welcome to JournalDev
"
convenience required:
protocol MyInitialiser {
init(name: String)
}
class TestInitProtocol : MyInitialiser{
convenience required init(name: String) {
print("Welcome to (name)")
self.init()
}
}
var t = TestInitProtocol(name: "JournalDev")
4.使用枚举实现协议
protocol ModifyCurrentDay
{
func currentDay() -> String
mutating func firstDayOfTheWeek()
}
enum DaysOfAWeek: String,ModifyCurrentDay{
case Sunday
case Monday
case Tuesday
case Wednesday
func currentDay()->String{
return self.rawValue
}
mutating func firstDayOfTheWeek() {
self = .Sunday
}
}
var day = DaysOfAWeek.Wednesday
print(day.currentDay()) //prints "Wednesday
"
day.firstDayOfTheWeek()
print(day.currentDay()) //prints "Sunday
"