blog icon indicating copy to clipboard operation
blog copied to clipboard

swift4语法备忘- Class and Structures

Open waltcow opened this issue 6 years ago • 0 comments

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.

  • 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

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

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 (===)

  • Not identical to (!==)

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:

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

waltcow avatar Apr 16 '18 09:04 waltcow