waltcow / blog

A personal blog
21 stars 2 forks source link

swift4语法备忘- Class and Structures #29

Open waltcow opened 6 years ago

waltcow commented 6 years ago

Classes and Structures

Concept

Classes and structures are general-purpose, flexible constructs that become the building blocks of your program’s code. You define properties and methods to add functionality to your classes and structures by using exactly the same syntax as for constants, variables, functions.

Comparing Classes and Structures

Classes and structures things in common.

Classes have additional capabilities that structures do not:

Syntax

class SomeClass {
    // class definition goes here
}
struct SomeStructure {
    // structure definition goes here
}

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

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

Class and Structure Instances

Syntax for creating instances is very similar for both structures and classes:

let someResolution = Resolution()
let someVideoMode = VideoMode()

Accessing Properties

Access the properties of an instance using dot syntax. In dot syntax, you write the property name immediately after the instance name, separated by a period (.), without any spaces:

print("The width of someResolution is \(someResolution.width)")
// Prints "The width of someResolution is 0"

someVideoMode.resolution.width = 1280

print("The width of someVideoMode is now \(someVideoMode.resolution.width)")
// Prints "The width of someVideoMode is now 1280"

Memberwise Initializers for Structure Types

All structures have an automatically-generated memberwise initializer, which you can use to initialize the member properties of new structure instances. Initial values for the properties of the new instance can be passed to the memberwise initializer by name:

let vga = Resolution(width: 640, height: 480)

Class instances do not receive a default memberwise initializer.

Structures and Enumerations Are Value Types

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.

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.

Classes Are Reference 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.

Identity Operators

Swift provides two identity operators:

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.

When should you choose to use Structures

Consider creating a structure when one or more of these conditions apply: