swift基礎之協議

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//無參數,有返回值
}

類型方法

相當於OC類方法 需要在方法名前加 static

protocol SomeProtocol {
   
static func Funtion(value:String) ->String
}

構造器  遵循構造器必須 使用 必須關鍵字 required 

protocol SomeProtocol {
   
init(name:String)
}

protocol someProtocol {
   
var name:String { get }     //屬性
    var age :Int { getset }
   
func fun(value:String) -> String        //實例方法
    staticfunc fun2(value:String) ->String//類型方法
    init(name:String)                       //構造器
}

實現協議
class SomeClass: someProtocol {

    var 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
    }
}

協議的類型
protocol Protocol {
   
}
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、利用協議是吸納代理模式

    實例中提供的功能,實際上是內部引用的其他實例實現的
可以用來實現 發送請求,寫入數據可等 非常靈活

//定義協議
protocol oneProtocol{
   
   
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、協議繼承

一個協議繼承另一個協議,相當於定義了父協議中的屬性和方法

protocol fatherProtocol {
   
func fatherFunction(value:String)
}
protocol sonProtocol : fatherProtocol {
   
func sonFunction(value:String) -> String
}
實現協議
class useProtocol: sonProtocol {
   
func fatherFunction(value: String) {
       
print("value")
    }
   
func sonFunction(value: String) -> String {
       
return  "這個值" + value
    }
}

3、類的專屬協議

   只用類才能實現的協議,在協議後面繼承 關鍵字 class 表明這個協議就就表明只能有類去實現,使用結構體實現就會失敗

protocol classOnlyProtocol : class,sonProtocol{
   
}
class onlyClass: classOnlyProtocol {
   
func fatherFunction(value: String) {
       
print(value)
    }
   
func sonFunction(value: String) -> String {
       
return value
    }
}

4、協議的合成 

協議的合成並不會生成一個新的類型,而是將多個協議合成一個臨時的協議,超出範圍就會立即失效

var dsadsa : protocol<oneProtocol,twoProtocol> 這就是一個合成的臨時的協議


protocol oneProtocol {
   
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()
testComponseProtocol(myCar)   
輸出:

30 50


5、協議檢查

使用 is  檢查類型返回bool值 當然也能檢查是否遵循某個協議
myCar is oneProtocol
結果返回true,因爲它繼承了oneProtocol

6、協議擴展

使用協議擴展的方式可以爲遵循者提供方法或者屬性的實現
通過這個方式可以讓我們無需每個遵循者都實現一次,無需使用全局函數
a、還可以爲擴展提供一個默認的實現,
b、爲協議的擴展添加限制條件  
擴展的語法
使用 關鍵字where ,Self  注意不是self,首字母大寫,裏面函數實現使用 self

protocol oneProtocol {
   
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
    }
}
//添加一個限制條件
使用 關鍵字where ,Self  注意不是self,首字母大寫,裏面函數實現使用 self

extension oneProtocol where Self :twoProtocol{
    func isSpeedBigger() -> Bool {
       
return self.maxSpeed > self.topSpeed
    }
}

struct MyCar:oneProtocol,twoProtocol {//不用實現協議中屬性,因爲擴展協議提供了一個默認的屬性實現
}

func testComponseProtocol(type: protocol<oneProtocol,twoProtocol>) {
   
print(type.topSpeed,type.maxSpeed)
}
var myCar = MyCar()
testComponseProtocol(myCar)


輸出: 20 60
myCar.maxSpeed //60
myCar
.topSpeed //20
myCar.isSpeedBigger() //true



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章