swift类和结构体(二)

枚举和结构体都是值类型
A value type is a type whose value is copied when it is assigned to a variable or constant, or when it is passed to a function.
值类型是:当他被赋值给常量或者变量的时候,它的值是被拷贝一份的,即不是引用。
当用它来赋值的时候,他的所有的值类型属性都会被拷贝一份。
类是引用类型
Unlike value types, reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used instead.
不像只类型,当他们被赋值给常量或者变量的时候,或者他们被传递给函数的时候。不是拷贝,应用的是已经存在的那个实例。
恒等运算符
Because classes are reference types, it is possible for multiple constants and variables to refer to the same single instance of a class behind the scenes. (The same is not true for structures and enumerations, because they are always copied when they are assigned to a constant or variable, or passed to a function.)
由于类是引用类型,可能多个变量或者常量引用的是同一个类的实例。但是对于结构体,这却是不可能的,因为它们进行的总是拷贝。
It can sometimes be useful to find out if two constants or variables refer to exactly the same instance of a class. To enable this, Swift provides two identity operators:
有时候发现多个实例引用的是同一个实例是有意义的。为了达到这个目的,swift提供了两个恒等运算符。【===和!==】
注意
Note that “identical to” (represented by three equals signs, or ===) does not mean the same thing as “equal to” (represented by two equals signs, or ==):
同一和等于是不一样的。
“Identical to” means that two constants or variables of class type refer to exactly the same class instance.
同一意味着是同一个引用。
“Equal to” means that two instances are considered “equal” or “equivalent” in value, for some appropriate meaning of “equal”, as defined by the type’s designer.
等于意味着值相等。
指针
If you have experience with C, C++, or Objective-C, you may know that these languages use pointers to refer to addresses in memory. A Swift constant or variable that refers to an instance of some reference type is similar to a pointer in C, but is not a direct pointer to an address in memory, and does not require you to write an asterisk (*) to indicate that you are creating a reference. Instead, these references are defined like any other constant or variable in Swift.
swift里边的引用类似指针,但是并不是一个直接的指针指向内存。相反,他们被定义像常量或者变量一样。
类和结构体的选择
当达到一个或者多个标准可以考虑创建结构体:
The structure’s primary purpose is to encapsulate a few relatively simple data values.
It is reasonable to expect that the encapsulated values will be copied rather than referenced when you assign or pass around an instance of that structure.
Any properties stored by the structure are themselves value types, which would also be expected to be copied rather than referenced.
The structure does not need to inherit properties or behavior from another existing type.
1.结构体的主要目的是为了封装几种相关联的简单数据值。
2.希望在赋值或者在函数中作为参数进行传递的时候,进行的是拷贝而不是引用。
3.存储在结构体中的任何属性都是值类型,是拷贝而不是引用。
4.结构体并不需要继承任何属性或者方法。
In all other cases, define a class, and create instances of that class to be managed and passed by reference. In practice, this means that most custom data constructs should be classes, not structures.
在所有的其他情况下,都要定义一个类,创建类的实例。传递和管理都是引用。事实上,这意味着大多数自定义数据都应该是类而不是结构体。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值