Swift4 基础部分: Classes and Structures

本文是学习《The Swift Programming Language》整理的相关随笔,基本的语法不作介绍,主要介绍Swift中的一些特性或者与OC差异点。

系列文章:

  • Swift4 基础部分:The Basics
  • Swift4 基础部分:Basic Operators
  • Swift4 基础部分:Strings and Characters
  • Swift4 基础部分:Collection Types
  • Swift4 基础部分:Control Flow
  • Swift4 基础部分:Functions
  • Swift4 基础部分:Closures
  • Swift4 基础部分: Enumerations

比较类与结构体(Comparing Classes and Structures)

Classes and structures in Swift have many things in common. Both can:

- Define properties to store values
- Define methods to provide functionality
- Define subscripts to provide access to their values using subscript syntax
- Define initializers to set up their initial state
- Be extended to expand their functionality beyond a default implementation
- Conform to protocols to provide standard functionality of a certain kind
- 
For more information, see Properties, Methods, Subscripts, Initialization, Extensions, and Protocols.

Classes have additional capabilities that structures do not:

- Inheritance enables one class to inherit the characteristics of another.
- Type casting enables you to check and interpret the type of a class instance at runtime.
- Deinitializers enable an instance of a class to free up any resources it has assigned.
- Reference counting allows more than one reference to a class instances

Swift 中类和结构体有很多共同点。共同处在于:

  • 定义属性用于存储值
  • 定义方法用于提供功能
  • 定义附属脚本用于访问值
  • 定义构造器用于生成初始化值
  • 通过扩展以增加默认实现的功能
  • 符合协议以对某类提供标准功能

与结构体相比,类还有如下的附加功能:

  • 继承允许一个类继承另一个类的特征
  • 类型转换允许在运行时检查和解释一个类实例的类型
  • 解构器允许一个类实例释放任何其所被分配的资源
  • 引用计数允许对一个类的多次引用

定义语法(Definition Syntax)

Classes and structures have a similar definition syntax. 
You introduce classes with the class keyword and 
structures with the struct keyword. 

例子:

struct Resolution {
    var width = 0
    var height = 0
}

class VideoMode {
    var resolution = Resolution()
    var interlaced = false
    var frameRate = 0.0
    var name: String?
}

var someResolution = Resolution();
someResolution.width = 10;
someResolution.height = 10;
var someVideoMode = VideoMode();
someVideoMode.resolution = someResolution;
someVideoMode.frameRate = 1.0;
someVideoMode.name = "mp4";

print("someVideoMode.resolution: (\(someVideoMode.resolution.width) \(someVideoMode.resolution.height)),someVideoMode.name \(someVideoMode.name)");

执行结果:

someVideoMode.resolution: (10 10),someVideoMode.name Optional("mp4")

结构体与枚举都是值类型数据(Structures and Enumerations Are Value Types)

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.

结构体例子:

struct Resolution {
    var width = 0
    var height = 0
}

let hd = Resolution(width:1920,height:1080)
var cinema = hd
cinema.width = 2048
print("cinema is now \(cinema.width) pixels wide")
print("hd is still \(hd.width) pixels wide")

执行结果:

cinema is now 2048 pixels wide
hd is still 1920 pixels wide

枚举例子:

enum CompassPoint {
    case north, south, east, west
}
var currentDirection = CompassPoint.west
let rememberedDirection = currentDirection
currentDirection = .east
if rememberedDirection == .west {
    print("The remembered direction is still .west")
}

执行结果:

The remembered direction is still .west

类是引用类型数据(Classes Are Reference Types)

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.

例子:

struct Resolution {
    var width = 0
    var height = 0
}

class VideoMode {
    var resolution = Resolution()
    var interlaced = false
    var frameRate = 0.0
    var name: String?
}

var someVideoMode = VideoMode();
someVideoMode.name = "mp4";

var otherVideoMode = someVideoMode;
otherVideoMode.name = "AVI"
print("someVideoMode.name : (\(someVideoMode.name)");

执行结果:

someVideoMode.name : (Optional("AVI")

恒等式操作符(Identity Operators)

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:

Identical to (===)

Not identical to (!==)

接着上述的例子:

struct Resolution {
    var width = 0
    var height = 0
}

class VideoMode {
    var resolution = Resolution()
    var interlaced = false
    var frameRate = 0.0
    var name: String?
}

var someVideoMode = VideoMode();
someVideoMode.name = "mp4";

var otherVideoMode = someVideoMode;
otherVideoMode.name = "AVI"

var thirdVideoMode = VideoMode();

if someVideoMode === otherVideoMode{
    print("someVideoMode === otherVideoMode")
}

if someVideoMode !== thirdVideoMode{
    print("someVideoMode !== thirdVideoMode")
}

执行结果:

someVideoMode === otherVideoMode
someVideoMode !== thirdVideoMode

类和结构体的选择(Choosing Between Classes and Structures)

You can use both classes and structures to define custom data types to use as the building blocks of your program’s 
code.

However, structure instances are always passed by value, 
and class instances are always passed by reference. This 
means that they are suited to different kinds of tasks. As you consider the data constructs and functionality that 
you need for a project, decide whether each data construct 
should be defined as a class or as a structure.

As a general guideline, consider creating a structure when 
one or more of these conditions apply:

- 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.

Examples of good candidates for structures include:

- The size of a geometric shape, perhaps encapsulating a width property and a height property, both of type Double.

- A way to refer to ranges within a series, perhaps encapsulating a start property and a length property, both of type Int.

- A point in a 3D coordinate system, perhaps encapsulating x, y and z properties, each of type Double.


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.

按照通用的准则,当符合一条或多条以下条件时,请考虑构建结构体:

  • 结构体的主要目的是用来封装少量相关简单数据值。
  • 有理由预计一个结构体实例在赋值或传递时,封装的数据将会被拷贝而不是被引用。
  • 任何在结构体中储存的值类型属性,也将会被拷贝,而不是被引用。
  • 结构体不需要去继承另一个已存在类型的属性或者行为。

合适的结构体候选者包括:

  • 几何形状的大小,封装一个width属性和height属性,两者均为Double类型。
  • 一定范围内的路径,封装一个start属性和length属性,两者均为Int类型。
  • 三维坐标系内一点,封装x,y和z属性,三者均为Double类型。

字符串与集合类型的赋值和拷贝行为(Assignment and Copy Behavior for Strings, Arrays,and Dictionaries)

In Swift, many basic data types such as String, Array, and 
Dictionary are implemented as structures. This means that 
data such as strings, arrays, and dictionaries are copied 
when they are assigned to a new constant or variable, or 
when they are passed to a function or method.
  • 在Swift中String,Array,Dictionary都是以结构体的方式实现的。因此当使用它们赋值或者传入函数作为参数时都是以拷贝的方式传入。

例子:

var array:[Int] = [1,2,3]
var otherArray = array
otherArray.append(4)
print("array:\(array),otherArray:\(otherArray)")

执行结果:

array:[1, 2, 3],otherArray:[1, 2, 3, 4]

你可能感兴趣的:(Swift4 基础部分: Classes and Structures)