首页 > 代码库 > Swift - Protocols and Extensions
Swift - Protocols and Extensions
The Swift Programming Language中的代码加上部分EXPERIMENT
import UIKitprotocol 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.simpleDescriptionstruct SimpleStructure: ExampleProtocol { var simpleDescription: String = "A simple structure" mutating func adjust() { simpleDescription += " (adjusted)" }}var b = SimpleStructure()b.adjust()let bDescription = b.simpleDescriptionenum SimpleEnum: ExampleProtocol { case SIMPLE(String) var simpleDescription: String { get { switch self { case let .SIMPLE(str): return str } } } mutating func adjust() { switch self { case let .SIMPLE(str): self = .SIMPLE(str + " (adjusted)") } }}var c = SimpleEnum.SIMPLE("A simple enum")c.simpleDescriptionc.adjust()c.simpleDescriptionextension Int: ExampleProtocol { var simpleDescription: String { return "The number \(self)" } mutating func adjust() { self += 42 }}var d = 7d.simpleDescriptiond.adjust()let protocolValue: ExampleProtocol = aprotocolValue.simpleDescription
Swift - Protocols and Extensions
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。