swift学习笔记 -- 协议和扩展
2015-12-18 14:54
375 查看
print("Hello, Swift!")
//使用 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
print(aDescription)
print(bDescription)
//注意在 SimpleStructure
声明的时候,使用 mutating
关键字标记一个会修改结构体的方法。SimpleClass
的声明不需要方法标记,因为类中的方法经常会修改类。
//使用 extension
为现有的类型添加功能,比如添加一个计算属性的方法。你可以在任何地方使用 extension
来给任意类型添加协议,甚至是从外部库或者框架中导入的类型。
extension Int:
ExampleProtocol {
var simpleDescription:
String {
return
"The number \(self)"
}
mutating func adjust() {
self += 42
}
}
print(7.simpleDescription)
//你可以像使用其他命名类型一样,使用一个接口名称
:例如,创建一个有不同类型但是都实现同一个接口的对象集合。当你处理接口类型的值时,接口外定义的方法不可用。
let protocolValue:
ExampleProtocol = a
print(protocolValue.simpleDescription)
// protocolValue.anotherProperty // Uncomment to see the error
//即使 protocolValue
变量在运行时的类型是 simpleClass,但是编译器会把它当做 ExampleProtocol
类型。这表示你不能调用实现此接口的类中的自有的方法或者属性。
//使用 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
print(aDescription)
print(bDescription)
//注意在 SimpleStructure
声明的时候,使用 mutating
关键字标记一个会修改结构体的方法。SimpleClass
的声明不需要方法标记,因为类中的方法经常会修改类。
//使用 extension
为现有的类型添加功能,比如添加一个计算属性的方法。你可以在任何地方使用 extension
来给任意类型添加协议,甚至是从外部库或者框架中导入的类型。
extension Int:
ExampleProtocol {
var simpleDescription:
String {
return
"The number \(self)"
}
mutating func adjust() {
self += 42
}
}
print(7.simpleDescription)
//你可以像使用其他命名类型一样,使用一个接口名称
:例如,创建一个有不同类型但是都实现同一个接口的对象集合。当你处理接口类型的值时,接口外定义的方法不可用。
let protocolValue:
ExampleProtocol = a
print(protocolValue.simpleDescription)
// protocolValue.anotherProperty // Uncomment to see the error
//即使 protocolValue
变量在运行时的类型是 simpleClass,但是编译器会把它当做 ExampleProtocol
类型。这表示你不能调用实现此接口的类中的自有的方法或者属性。
相关文章推荐
- swift 可选值?
- Swift学习笔记 -- 枚举和结构
- Swift学习之路 -- 协议、扩展和泛型
- Swift 中的 Runtime
- Swift之正则表达式
- Swift 中的 Runtime
- 升级Xcode7之后安装的插件不能使用的解决办法
- Swift 字符串和字符(笔记)
- IOS UIButton UILabel 等title 添加下划线 swift 版
- swift学习记录(闭包)
- iOS-swift-动画 简单基础的动画摘要-1
- Swift -- 函数的使用(函数作为参数、返回值、嵌套函数)
- swift基本用法-for循环遍历,遍历字典,循环生成数组
- 深入探究Swift数组背后的协议、方法、拓展
- Swift之便利构造函数
- Swift Hello World、 注释、分号
- Swift流程控制之循环语句和判断语句详解
- 怎样在swift中使用cocoapods导入的第三方oc库
- Swift学习笔记1
- 为swift编码引入map()和flatMap(), map those arrays