protocol Description {
var description:String{get}
func test() -> String
}
struct Test{
var name: String
var description: String{
return "name is \(name)"
}
func test() -> String {
return "test"
}
}
extension Test:Description{}
var a = Test(name: "小明")
print(a.description)
var b: Description = a
print(b.test())
協議類型集合
protocol Description {
var description: String{get}
}
class Animal: Description {
var description: String{
return "Animal type"
}
}
class Dog: Description {
var description: String{
return "Dog type"
}
}
var a: [Description] = [Animal(), Dog()]
for item in a {
print(item.description)
}
協議的繼承
protocol Description {
var description: String{get}
}
protocol Test: Description {
var test: String{get}
}
class Animal: Test {
var description: String{
return "Animal type"
}
var test: String{
return "test"
}
}
var aAnimal = Animal()
print(aAnimal.test)
print(aAnimal.description)
類專屬協議
protocol Description {
var description: String{get}
}
protocol Test: AnyObject, Description {
var test: String{get}
}
//下邊結構體將會報錯
struct Dog: Test {
var description: String{
return "Dog type"
}
var test: String{
return "test"
}
}
class Animal: Test {
var description: String{
return "Animal type"
}
var test: String{
return "test"
}
}
var aAnimal = Animal()
print(aAnimal.test)
print(aAnimal.description)
協議合成
protocol Name {
var name: String{get}
}
protocol Age {
var age: Int{get}
}
struct Person: Name, Age {
var name: String
var age: Int
}
func description(person: Name & Age){
print("name is \(person.name) age is \(person.age)")
}
description(person: Person(name: "小明", age: 23))
判斷是遵守了哪種協議
as? : 用來判斷是否遵守了某個協議,如果沒遵守就返回nil,如果遵守了,就返回這個協議。
as!和 as?一樣但是有可能觸發運行時錯誤。
protocol AreaProtocol {
var area: Double {get}
}
class Circle: AreaProtocol {
let pi = 3.1415926
var ridus: Double
var area: Double {
return pi * ridus * ridus
}
init(ridus: Double) {
self.ridus = ridus
}
}
class Country: AreaProtocol {
var area: Double
init(area: Double) {
self.area = area
}
}
class Animal {
var legs: Int
init(legs: Int) {
self.legs = legs
}
}
var objs: [AnyObject] = [
Circle(ridus: 5.0),
Country(area: 1300),
Animal(legs: 4)
]
for item in objs {
if let areaPro = item as? AreaProtocol {
print("area = \(areaPro.area)")
}else{
print("no area")
}
}
可選協議要求
@objc用在協議前面
@objc optional用在屬性和方法前面
這種表明只有oc的類纔可以遵守該協議。
import UIKit
@objc protocol CountDataSource {
@objc optional func incerment(count: Int) -> Int
@objc optional var countIncerment: Int{get}
}
class Count {
var total = 0
var dataSource: CountDataSource?
func increment(){
if let amount = dataSource?.incerment?(count: total){
total += amount
}else if let amount = dataSource?.countIncerment{
total += amount
}
}
}
class CountObj: NSObject, CountDataSource{
var countIncerment: Int{
return 3
}
}
//var a = Count()
//a.dataSource = CountObj()
//for _ in 1...4 {
// a.increment()
// print(a.total)
//}
class ZeroSource: NSObject, CountDataSource {
func incerment(count: Int) -> Int {
switch count {
case 0:
return 0
case let cot where cot > 0:
return -1
case let cot where cot < 0:
return 1
default:
return 0
}
}
}
var b = Count()
b.dataSource = ZeroSource()
b.total = -4
for _ in 1...5{
b.increment()
print(b.total)
}
協議的擴展
可以給協議擴展計算屬性,和方法。
被擴展的協議是可選的協議了,在類裏面可以直接使用。
import UIKit
protocol TestProtocol {
func random() -> Double
}
extension TestProtocol {
var probability: Bool {
return random() >= 0.5
}
}
class TestClass: TestProtocol {
func random() -> Double {
let num = arc4random_uniform(100 - 0) + 0
let count = Double(num % 10) * 0.1
print(count)
return count
}
}
var a = TestClass()
print(a.probability)
爲協議擴展添加約束條件
extension Collection where Element: Equatable{
func allEqual() -> Bool {
for item in self {
if item != self.first {
return false
}
}
return true
}
}
var a = [1,1,1,1,1]
var b = [1,1,1,1,1]
var c = [1,1,1,1,1,2]
print(a.allEqual())
print(b.allEqual())
print(c.allEqual())