swift基礎之協議
分爲:
一、協議初步
1、協議的定義
2、協議的語法
3、協議對屬性,方法,構造器的規定
4、協議類型
二、協議進階
1、利用協議實現代理模式
2、協議繼承
3、類的專屬協議
4、協議合成
5、協議檢查
6、協議擴展
一、協議初步
1、協議的定義
協議類似於其他語言的接口,用來規定要實現的某些特定功能必須的屬性和方法
類、結構體、枚舉都可以實現協議規定的方法和屬性 叫做遵循協議
使用方法: 冒號“ : ” 協議的名字
遵循多個協議,用逗號“ , ” 隔開
2、語法
protocolSomeProtocol {
//協議內容
//規定的屬性,方法、構造器
}
3、協議對屬性,方法,構造器的規定
//協議對屬性的規定
protocoloneProtocol {
var canFly :Bool{get}//只讀
var name :String
{get set}//讀寫
var amplitude :Double{get set}
var frequency :Double{get set}
}
//協議對方法的規定
要求實現特定的實例方法或者類方法,不支持參數默認值
實例方法,
只需要指定函數名,參數,返回值 不需要括號
protocol FlyProtocol {
func fly()//無參數,無返回值
func speed() ->Double//無參數,有返回值
}
類型方法
static func Funtion(value:String) ->String
構造器 遵循構造器必須 使用 必須關鍵字 required
init(name:String)
var name:String { get } //屬性
var age :Int { getset }
func fun(value:String) -> String //實例方法
staticfunc fun2(value:String) ->String//類型方法
init(name:String) //構造器
var age: Int
func fun(value: String) -> String {
return value + name
}
static func fun2(value: String) -> String {
return value
}
required init(name: String) {
self.name = name
self.age = 0
}
}
class A: Protocol {
}
class B: Protocol {
}
var a = A()
var b = B()
var typeArray:[Protocol] = [a,b]
typeArray.dynamicType //Array<Protocol>.Type
for item intypeArray{
iflet one = item as? A{
}
elseiflet two = item as? B{
}
二、協議進階
1、利用協議是吸納代理模式
func nslog(value:String)
}
class oneClassDelegate: oneProtocol { //B
func nslog(value: String) {
print("輸出信息\(value)")
}
}
//定義協議類型,調用代理協議的方法
class MyClassMake { //A
var delegate:oneProtocol?//一個類型協議
func logMessage(value:String) {
delegate?.nslog(value)
}
}
var myClassMake = MyClassMake() //實例類A
myClassMake.delegate = oneClassDelegate() ///類裏面協議屬性是一個繼承協議的實例類B
myClassMake.logMessage("hello world") //A調用自身的方法,裏面調用的是協議的方法
輸出信息 hello world
//B類在其中就是協議的代理實現,A類就調用B類裏面代理方法
2、協議繼承
一個協議繼承另一個協議,相當於定義了父協議中的屬性和方法
func fatherFunction(value:String)
}
protocol sonProtocol : fatherProtocol {
func sonFunction(value:String) -> String
func fatherFunction(value: String) {
print("value")
}
func sonFunction(value: String) -> String {
return "這個值" + value
}
3、類的專屬協議
只用類才能實現的協議,在協議後面繼承 關鍵字 class 表明這個協議就就表明只能有類去實現,使用結構體實現就會失敗
}
class onlyClass: classOnlyProtocol {
func fatherFunction(value: String) {
print(value)
}
func sonFunction(value: String) -> String {
return value
}
4、協議的合成
協議的合成並不會生成一個新的類型,而是將多個協議合成一個臨時的協議,超出範圍就會立即失效
var topSpeed:Int { get }
}
protocol twoProtocol {
var maxSpeed:Int { get }
}
struct MyCar:oneProtocol,twoProtocol {
var topSpeed: Int = 30
var maxSpeed: Int = 50
}
這個函數裏面的參數就是一個合成的臨時協議
func testComponseProtocol(type: protocol<oneProtocol,twoProtocol>) {
print(type.topSpeed,type.maxSpeed)
}
var myCar = MyCar()
30 50
5、協議檢查
6、協議擴展
var topSpeed:Int { get }
}
protocol twoProtocol {
var maxSpeed:Int { get }
}
extension oneProtocol{ //擴展協議 提供了一個默認的屬性實現
var topSpeed:Int {
get {
return 20
}
}
}
extension twoProtocol{ //擴展協議 提供了一個默認屬性的實現
var maxSpeed: Int {
return 60
}
}
return self.maxSpeed > self.topSpeed
}
}
func testComponseProtocol(type: protocol<oneProtocol,twoProtocol>) {
print(type.topSpeed,type.maxSpeed)
}
var myCar = MyCar()
myCar.topSpeed //20