import ArgumentParser @propertyWrapper struct WrapperTest { internal var innerValue: Int { didSet { print("didSet called \(oldValue)") } willSet { print("willSet called \(newValue)") } } public var wrappedValue: Int { get { return innerValue } set { innerValue = min(50, newValue) print("setter called") } } // 装饰器的初始化器,可以初始默认值 init(wrappedValue initialValue: Int) { self.innerValue = 0 self.wrappedValue = initialValue } } struct WrapperTestDemo { @WrapperTest(wrappedValue: 55) var demo1: Int @WrapperTest() var demo2: Int = 88 } let demo = WrapperTestDemo() print(demo.demo1) print(demo.demo2)
原文地址:https://www.cnblogs.com/doudouyoutang/p/14483390.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。