Swift之协议

协议规定了用来实现某一特定任务或者功能的方法、属性,以及其他需要的东西。类、结构体或枚举都可以遵循协议,并为协议定义的这些要求提供具体实现。

一、协议语法(定义方式与类、结构体和枚举的定义相似)

protocol MyProtocol {
    // 协议定义的内容
}

二、遵循协议
1:遵循多个协议时,各协议之间用逗号隔开

// 类遵循协议 MyProtocol
class WriteDemo: MyProtocol {
    
}

// 结构体遵循协议 MyProtocol
struct ShowHeart:MyProtocol {
    
}

// 类遵循多个协议
class WriteDemo: MyProtocol, AnotherProtocol {
    
}

三、协议&属性
1、如果协议中定义了属性,必须要求遵循协议的类型,提供对应名称和类型的属性;
2、协议还指定属性是可读的还是可读可写的;
3、协议总是用 var 关键字来声明变量属性,在类型声明后加上 { set get } 来表示属性是可读可写,可读属性则用 { get } 来表示。

protocol MyProtocol {
    var name: String {set get}
}

class WriteDemo: MyProtocol {
    var name: String 
    init(name: String) {
        self.name = name
    }
}

struct ShowHeart:MyProtocol {
    var name: String
}

let writeDemo = WriteDemo(name: "Xiao Wei")
let showHeart = ShowHeart(name: "Li Hua")

四、协议&方法
1、协议可以要求遵循协议的类型实现该协议指定的实例方法或类方法;
2、方法作为协议的一部分,像普通方法一样放在协议的定义中,但是不需要大括号和方法体
3、在协议中定义类方法的时候,总是使用 static 关键字作为前缀。

实例方法:

protocol MyProtocol {
    func PrintWorld()
}

class WriteDemo: MyProtocol {
    func PrintWorld() {
        
    }
}

struct ShowHeart:MyProtocol {
    func PrintWorld() {
        
    }
}

let writeDemo = WriteDemo()
writeDemo.PrintWorld()

类方法(使用 static 关键字作为前缀):

protocol MyProtocol {
    static func PrintWorld()
}

class WriteDemo: MyProtocol {
    static func PrintWorld() {
        
    }
}

struct ShowHeart:MyProtocol {
    static func PrintWorld() {
        
    }
}

WriteDemo.PrintWorld()

五、协议 & 构造器
协议要求遵循协议的类型实现协议中指定的构造器,必须为构造器实现标上 required 修饰符

protocol MyProtocol {
    init()
}
class WriteDemo: MyProtocol {
    required init() {
    }
}

如果类已经被标记为 final,那么不需要在协议构造器的实现中使用 required 修饰符,因为 final 类不能有子类:

protocol MyProtocol {
    init()
}

final class WriteDemo: MyProtocol {
    init() {
        
    }
}

如果一个子类重写了父类的指定构造器,并且该构造器满足了某个协议的要求,那么该构造器的实现需要同时标注 required 和 override 修饰符:

protocol MyProtocol {
    init()
}

class ShowTheWorld: NSObject {
    override init() {
        
    }
}

class WriteDemo: ShowTheWorld, MyProtocol {
    required override init() {
        
    }
}

六、协议作为类型
1、协议可以被当做一个功能完备的类型来使用;
2、协议是一种类型,因此协议类型的名称应与其他类型(例如 Int,Double,String)的写法相同,使用大写字母开头的驼峰式写法。

protocol MyProtocol {
    
}

class ShowTheWorld: NSObject {
    let myProtocol : MyProtocol
    init(myProtocol: MyProtocol) {
        self.myProtocol = myProtocol
    }
}

七、委托

八、扩展遵循协议
扩展可以为已有类型添加属性、方法、下标以及构造器。

通过ShowTheWorld的扩展为ShowTheWorld类添加属性:

protocol MyProtocol {
    var name :String {get}
}

class ShowTheWorld {
    
}

extension ShowTheWorld: MyProtocol{
    var name: String{
        return "name"
    }
}

let showWorld = ShowTheWorld()
showWorld.name
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容