9. Swift Structures and Classes

    技术2022-07-10  103

    Definition Syntax

    Here’s an example of a structure definition and a class definition: struct Resolution { var width = 0 var height = 0 } class VideoMode { var resolution = Resolution() var interlaced = false var frameRate = 0.0 var name: String? }

    Declaring instances

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

    Accessing Properties

    You can access the properties of an instance by using dot syntax print("the weight of resolution is \(someResolution.weight)") print("the height of resolution is \(someResolution.height)") You can access the subproperties print("the weight of resolution of VideoMode is \(someVideoMode.resolution.weight)")

    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 instancesInitial values for the properties of the new instance can be passed to the memberwise initializer by name let vga = Resolution(weight: 1, height: 2) Unlike structures, class instances don’t receive a default memberwise initializer

    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.All of the basic types in Swift—integers, floating-point numbers, Booleans, strings, arrays and dictionaries—are value types var hd = Resolution(weight: 1080, height: 1920) var cinema = hd cinema.height = 2048 print(hd.height) print(cinema.height)

    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. let tenEighty = VideoMode() tenEighty.resolution = hd tenEighty.interlaced = true tenEighty.name = "1080i" tenEighty.frameRate = 25.0 let alsoTenEighty = tenEighty alsoTenEighty.frameRate = 30.0

    Note that tenEighty and alsoTenEighty are declared as constants, rather than variables. However, you can still change tenEighty.frameRate and alsoTenEighty.frameRate because the values of the tenEighty and alsoTenEighty constants themselves don’t actually change. tenEighty and alsoTenEighty themselves don’t “store” the VideoMode instance—instead, they both refer to a VideoMode instance behind the scenes. It’s the frameRate property of the underlying VideoMode that is changed, not the values of the constant references to that VideoMode.

    Identity Operators

    It can sometimes be useful to find out whether 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 (!==) if tenEighty === alsoTenEighty { print("they refer to the same instance.") }

    general term

    general-purpose 多功能的instance n.实例inheritance n.继承cast v.转换interpret v.解释
    Processed: 0.136, SQL: 9