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
}
}
Swift学习小结之协议和扩展
最新推荐文章于 2024-10-05 15:32:05 发布