加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift之旅(六)协议与扩展

发布时间:2020-12-14 02:13:08 所属栏目:百科 来源:网络整理
导读:使用 protocol 来定义一个协议。 protocol ExampleProtocol { var simpleDescription: String { get } mutating func adjust()} 类、枚举和结构体都可以接受协议。 class SimpleClass: ExampleProtocol { var simpleDescription: String = "A very simple cl

使用 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

试一试

写一个遵循该协议的枚举。

注意SimpleStructure里使用了 mutating 关键字标记了一个修改结构体的方法。SimpleClass的定义里不需要任何方法标记为mutating,因为类中的方法总是会修改这个类的。

使用 extension 来为已经存在的类增加功能,比如新方法和计算过的属性。你可以用 extension 给那些定义在别的地方的类型增加协议遵循,甚至可以加到导入的类库或者框架的类型中。

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

试一试

为Double类型写一个扩展,增加一个 absoluteValue 属性。

你可以像其它任何命名类型那样使用协议名——例如,要创建一个对象集合,对象的类型都不一样,不过都遵循一个单独的协议。当遇到类型为协议类型的值时,协议定义以外的方法是不可用的。

let protocolValue: ExampleProtocol = a
println(protocolValue.simpleDescription)
// println(protocolValue.anotherProperty) // 取消注释看看会有什么错误

即使变量 protocolValue 运行时类型是 SimpleClass,编译器还是把它当作给定的 ExampleProtocol 类型。这说明除了协议里定义的,你没法访问到类实现的方法和属性。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读