iOS编程语言-Swift中结构体和枚举是值类型

Structures and Enumerations Are Value Types

  • A value type is a type whose value is copied when it’s assigned to a variable or constant, or when it’s passed to a function.
  • In fact, all of the basic types in Swift-integers, floating-point numbers, Booleans, strings, arrays and dictionaries-are value types, and are implemented as structures behind the scenes.
  • All structures and enumerations are value types in Swift. This meas that any structure and enumeration instances you create-and any value types they have as properties-are always copied when they are passed around in your code.
An Example to describe the Essence
//声明结构体
struct Resolution {
      var width = 0
      var hetight = 0
}
//创建一个结构体实例
let hd = Resolution(width: 1920, height: 1080)
//将实例hd赋值给变量cinema
var cinema = hd
//修改变量cinema的宽度
cinema.width = 2048
//打印输出hd和cinema中的width
print("\(hd.widht)") //输出1920
print("\(cinema.widht)")//输出2048
  • When cinema was given the current value of hd, the values stored in hd were copied into the new cinema instance. The end result was two completely separate instances that contained the same numeric values. However, because they are separate instances, setting the width of cinema to 2048 doesn’t affect the width stored in hd, as shown in the figure below:
The Same Behavior applies to Enumerations:
//声明枚举
enum CompassPoint {
    case north, south, east, west
    mutating func turnNorth() {
		self = .north
	}
}
//创建当前方向实例
var currentDirection = CompassPoint.west
//记录当前方向
let rememberedDirection = currentDirection
//修改当前方向
currentDirection.turnNorth()
//打印输出
print("\(currentDirection)")//输出north
print("\(rememberedDirection)")//输出west
  • When rememberedDirection is assigned the value of currentDirection, it’s acutally set to a copy of that value. Changing the value of currentDirection thereafter doesn’t affect the copy of the original value that was stored in rememberedDirection.
Choosing Between Structures and Classes
  • Structures and classes are good choices for storing data and modeling behavior in your apps, but their similarities can make it difficult to choose one over the other.
  • Consider the following recommendations to help choose which option makes sence when adding a new data type to your app.
  • Using structures makes it easier to reason about a portion of your code without needing to consider the whole state of your app. Because structures are value types-unlike classes-local changes to a structure aren’t visible to the rest of your app unless you intentionally communicate those changes as part of the flow of your app. As a result, you can look at a section of code and be more confident that changes to instances in that secion will be made explicitly, rather than being made invisibly from a tangentially related function call.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值