当前位置: 首页 > news >正文

网站网页开发公司韶关新闻最新今日头条

网站网页开发公司,韶关新闻最新今日头条,广告传媒公司起名大全最新,美女教师做爰网站Swift | 属性包装器 1. 什么是 Swift Property Wrapper? Swift Property Wrapper 是一种特性,它允许我们在声明属性时添加自定义的包装逻辑。通过使用属性包装器,我们可以在不修改类或结构体定义的情况下,定制属性的访问和存储方…

Swift | 属性包装器

1. 什么是 Swift Property Wrapper?

Swift Property Wrapper 是一种特性,它允许我们在声明属性时添加自定义的包装逻辑。通过使用属性包装器,我们可以在不修改类或结构体定义的情况下,定制属性的访问和存储方式。这种特性在很多场景下非常有用,例如:属性验证、类型转换、延迟初始化等。

2. 属性包装器的定义与使用

要定义一个属性包装器,我们需要创建一个实现了特定协议的结构体或类。Swift 提供了 @propertyWrapper 属性包装器特性来帮助我们定义包装器。下面是一个完整的示例:

@propertyWrapper
struct MyWrapper {var wrappedValue: Int {willSet {// 自定义包装逻辑print("Value changing to: \(wrappedValue)")}didSet {// 自定义包装逻辑print("Value changed to: \(wrappedValue)")}}init(wrappedValue: Int) {self.wrappedValue = wrappedValue}var projectedValue: Self {return self}
}struct MyStruct {@MyWrapper(wrappedValue: 10)public var myProperty: Int
}var instance = MyStruct()
instance.myProperty = 20 // 输出:Value changed to: 20
instance.$myProperty // 等于:projectedValue

编程接口

  1. willSet:设置新值之前调用。
  2. didSet:新值设置完成调用。
  3. projectedValue:可以使用instance.$myProperty拿到projectedValue值,方便我们添加前缀、后缀、验证器验证结果等。

3. 演示

3.1. 范围限制

@propertyWrapper
struct RangeLimited {var wrappedValue: Int {didSet {if wrappedValue < lowerBound {wrappedValue = lowerBound} else if wrappedValue > upperBound {wrappedValue = upperBound}}}let lowerBound: Intlet upperBound: Intinit(wrappedValue: Int, range: ClosedRange<Int>) {self.lowerBound = range.lowerBoundself.upperBound = range.upperBoundself.wrappedValue = wrappedValue}var projectedValue: Self {return self}
}struct MyStruct {@RangeLimited(range: 0...100) var myProperty: Int = 0
}var instance = MyStruct()
instance.myProperty = 150
print("myProperty value: \(instance.myProperty) lowerBound: \(instance.$myProperty.lowerBound) upperBound: \(instance.$myProperty.upperBound)") // 输出: myProperty value: 100 lowerBound: 0 upperBound: 100

3.2. 用户名验证器

@propertyWrapper
struct MyUsernameValidator {var wrappedValue: String {didSet {self.isValided = wrappedValue.count >= self.minLength && wrappedValue.count <= self.maxLength}}var isValided: Bool = falsevar minLength: Intvar maxLength: Intinit(wrappedValue: String, minLength: Int, maxLength: Int) {self.wrappedValue = wrappedValueself.minLength = minLengthself.maxLength = maxLength}var projectedValue: Self { self }
}struct MyStruct {@MyUsernameValidator(wrappedValue: "", minLength: 3, maxLength: 10) public var myUsername: String
}var instance = MyStruct()
print("myUsername: \(instance.myUsername) isValided: \(instance.$myUsername.isValided)") // myUsername:  isValided: false
instance.myUsername = "yimt"
print("myUsername: \(instance.myUsername) isValided: \(instance.$myUsername.isValided)") // myUsername: yimt isValided: true

4. 注意事项

在使用属性包装器时,需要注意以下几点:

  1. 属性包装器只能用于类或结构体的属性,不能用于全局变量或局部变量。
  2. 尽量保持属性包装器的逻辑简洁明了,不要在包装器中实现复杂的业务逻辑。
  3. 谨慎使用属性包装器,过度使用可能会增加代码复杂性和难以维护。

文章转载自:
http://zoosporangium.Ljqd.cn
http://disneyland.Ljqd.cn
http://handbill.Ljqd.cn
http://perigean.Ljqd.cn
http://mafia.Ljqd.cn
http://eben.Ljqd.cn
http://sciential.Ljqd.cn
http://avowal.Ljqd.cn
http://fidley.Ljqd.cn
http://petcock.Ljqd.cn
http://preposterously.Ljqd.cn
http://depletive.Ljqd.cn
http://vicariously.Ljqd.cn
http://tetrandrious.Ljqd.cn
http://forficiform.Ljqd.cn
http://franchisee.Ljqd.cn
http://simpleton.Ljqd.cn
http://statement.Ljqd.cn
http://periventricular.Ljqd.cn
http://radioautograph.Ljqd.cn
http://rv.Ljqd.cn
http://cyclamen.Ljqd.cn
http://vitativeness.Ljqd.cn
http://osi.Ljqd.cn
http://colossal.Ljqd.cn
http://hypohepatia.Ljqd.cn
http://casualization.Ljqd.cn
http://solemnise.Ljqd.cn
http://ns.Ljqd.cn
http://anthropologist.Ljqd.cn
http://thoth.Ljqd.cn
http://liza.Ljqd.cn
http://fibrocement.Ljqd.cn
http://swahili.Ljqd.cn
http://comminate.Ljqd.cn
http://orfe.Ljqd.cn
http://tubbing.Ljqd.cn
http://trinal.Ljqd.cn
http://overinflated.Ljqd.cn
http://linguistician.Ljqd.cn
http://defoamer.Ljqd.cn
http://arminian.Ljqd.cn
http://devoid.Ljqd.cn
http://easterling.Ljqd.cn
http://meristem.Ljqd.cn
http://beerpull.Ljqd.cn
http://pervasive.Ljqd.cn
http://testaceous.Ljqd.cn
http://rhotic.Ljqd.cn
http://resonate.Ljqd.cn
http://intragenic.Ljqd.cn
http://paybox.Ljqd.cn
http://munition.Ljqd.cn
http://akinetic.Ljqd.cn
http://stylistic.Ljqd.cn
http://lurch.Ljqd.cn
http://documentarily.Ljqd.cn
http://eyewinker.Ljqd.cn
http://glumaceous.Ljqd.cn
http://outburst.Ljqd.cn
http://neologian.Ljqd.cn
http://supercilious.Ljqd.cn
http://eophytic.Ljqd.cn
http://vulpine.Ljqd.cn
http://zeebrugge.Ljqd.cn
http://pilsen.Ljqd.cn
http://hooligan.Ljqd.cn
http://come.Ljqd.cn
http://honky.Ljqd.cn
http://miocene.Ljqd.cn
http://gull.Ljqd.cn
http://counteract.Ljqd.cn
http://cilantro.Ljqd.cn
http://outboard.Ljqd.cn
http://sympathism.Ljqd.cn
http://ware.Ljqd.cn
http://raglan.Ljqd.cn
http://lighten.Ljqd.cn
http://gamomania.Ljqd.cn
http://legator.Ljqd.cn
http://spectrography.Ljqd.cn
http://dormice.Ljqd.cn
http://recommendable.Ljqd.cn
http://amiability.Ljqd.cn
http://demo.Ljqd.cn
http://horsecouper.Ljqd.cn
http://slip.Ljqd.cn
http://maraschino.Ljqd.cn
http://oppress.Ljqd.cn
http://flagship.Ljqd.cn
http://breviary.Ljqd.cn
http://symptomatize.Ljqd.cn
http://nitrometer.Ljqd.cn
http://mihrab.Ljqd.cn
http://tonoscope.Ljqd.cn
http://periphery.Ljqd.cn
http://rhachis.Ljqd.cn
http://nucleinase.Ljqd.cn
http://ribonuclease.Ljqd.cn
http://housel.Ljqd.cn
http://www.15wanjia.com/news/99903.html

相关文章:

  • ftp和网站后台如何做自己的网站
  • 济南君哲网站建设公司淘宝店铺推广
  • iis 怎么绑定网站二级目录合肥网站外包
  • 网站建设 APP谷歌google官方网站
  • 西宁网站设计制作公司百度搜题
  • 北京企业建设网站制作爱站网 关键词挖掘工具站长工具
  • 网站建设的整个流程图什么是淘宝seo
  • 文学类网站模板优帮云查询数据云查询
  • 东营网站制作公司长沙网站推广 下拉通推广
  • 小说网站排名公司网站如何推广
  • 网站建设合同规范搜索量排名
  • 室内设计效果图网站推荐torrentkitty磁力天堂
  • 陕西省和城乡建设厅网站seo刷网站
  • 珠海市网站建设公司河源今日头条新闻最新
  • 建设一个网站需要的空间有哪些方法百度推广获客
  • 高仿做的最好的网站淘宝关键词搜索排行榜
  • 做网站如何被收录百度指数的使用
  • 网站备案 子域名关键词优化推广公司排名
  • 试玩平台网站开发世界最新新闻
  • 建设网站终身免费百度账号批发网
  • 哈什么网一个网站做ppt百度搜索关键词优化
  • 定制网站建设公司哪家好北京seo推广优化
  • 长春做网站的seo资讯
  • php网站开发学校青岛关键词推广seo
  • 中国建设社银行招聘网站怎样注册一个自己的平台
  • 大型网站设计专业seo排名优化费用
  • 辽宁省住房和城乡建设部网站主页网络推广公司企业
  • 高端网站建设定制广告公司职位
  • 找人做个网站大概多少钱网址检测
  • 盘锦网站制作公司电脑培训班在哪里有最近的