• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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人参与评论

请发表评论

全部评论

专题导读
上一篇:
swift3.0 代码创建经典界面的九宫图--优化篇发布时间:2022-07-14
下一篇:
一步一步搭建 Perfect Swift Server 服务器 (一)发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap