前端之家收集整理的这篇文章主要介绍了
Swift学习小结之协议和扩展,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import UIKit
var str = "Hello,playground"
// 协议和扩展
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
extension Int : ExampleProtocol{
var simpleDescription: String{
return "The number \(self)"
}
mutating func adjust() {
self += 42
}
}
原文链接:https://www.f2er.com/swift/327550.html