Swift学习——A Swift Tour 协议和扩展

Protocols and Extensions


Protocols  协议的使用

使用关键字 protocol 定义一个协议

protocol ExampleProtocol {
    var simpleDescription: String { get }
    mutating func adjust()
}

类,枚举和结构体都可以实现协议

class SimpleClass: ExampleProtocol {
    var simpleDescription: String = "A very simple class."
    var anotherProperty: Int = 69105
    func adjust() {
        simpleDescription += "  Now 100% adjusted."
    }
}
var a = SimpleClass()
a.adjust()
let aDescription = a.simpleDescription
 
struct SimpleStructure: ExampleProtocol {
    var simpleDescription: String = "A simple structure"
    mutating func adjust() {
        simpleDescription += " (adjusted)"
    }
}
var b = SimpleStructure()
b.adjust()
let bDescription = b.simpleDescription

在结构体中需要使用 mutating 关键字来标记实现的协议方法,在类中不需要这个关键字

协议主要的使用场合:

1. 需要由别的类实现的方法

2. 声明位置类的接口

3. 两个类之间通信



Extensions  扩展的使用

可以使用 extension 关键字为一个类型拓展协议,增加方法和属性

extension Int: ExampleProtocol {
    var simpleDescription: String {
    return "The number \(self)"
    }
    mutating func adjust() {
        self += 42
    }
}
7.simpleDescription

可以使用协议的名称作为数据类型(具体怎么用,没看明白,可以看官方英文版)

let protocolValue: ExampleProtocol = a
protocolValue.simpleDescription
// protocolValue.anotherProperty  // Uncomment to see the error



Generics   泛型的使用

使用   < >  声明泛型函数或者泛型类型

func repeat<ItemType>(item: ItemType, times: Int) -> ItemType[] {
    var result = ItemType[]()
    for i in 0..times {
        result += item
    }
    return result
}
repeat("knock", 4)

也可以在类,枚举,结构体中使用泛型

// Reimplement the Swift standard library's optional type
enum OptionalValue<T> {
    case None
    case Some(T)
}
var possibleInteger: OptionalValue<Int> = .None
possibleInteger = .Some(100)

有的时候需要对函数的参数类型进行详细的定义,对泛型实现某个接口,继承自某个特定类型,两个泛型属于同一个类型等要求,使用  where 关键字进行定义

func anyCommonElements <T, U where T: Sequence, U: Sequence, T.GeneratorType.Element: Equatable, T.GeneratorType.Element == U.GeneratorType.Element> (lhs: T, rhs: U) -> Bool {
    for lhsItem in lhs {
        for rhsItem in rhs {
            if lhsItem == rhsItem {
                return true
            }
        }
    }
    return false
}
anyCommonElements([1, 2, 3], [3])

如果条件简单,还可以简化写,比如<T: Equatable>和“<T where T: Equatable>是一样的


入门就说到这,下一节我们说说 Language Guide

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值