swift结构体的mutating

Structures and enumerations are value types. By default, the properties of a value type cannot be modified from within its instance methods.
结构体和枚举都是值类型。默认的,在实例方法内部,值类型的属性不能被修改。
However, if you need to modify the properties of your structure or enumeration within a particular method, you can opt in to mutating behavior for that method. The method can then mutate (that is, change) its properties from within the method, and any changes that it makes are written back to the original structure when the method ends. The method can also assign a completely new instance to its implicit self property, and this new instance will replace the existing one when the method ends.
然而,如果你需要在一个方法内部修改一个结构体或者枚举的属性。你可以选择mutating那个方法的行为。这个方法然后就可以变异在方法内部修改属性值。当方法结束的时候,任何的变化都会返回到原始结构里边。这个方法也可以利用他的隐式self属性赋值给一个新值。
这里写图片描述
分析
因为你已经申明了结构体实例是常量,因此他的素偶偶属性都不能进行更改了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值