Welcome to FutureAppLaboratory

v=(*^ワ^*)=v

Learning Swift Part 8 - Classes & Structures

| Comments

===== Full Notes =====

Classes and Structures

  • Comparing Classes and Structures (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
  • 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.
  • Definition Syntax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    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
1
2
    let someResolution = Resolution()
    let someVideoMode = VideoMode()
  • Accessing Properties
1
2
3
4
5
6
7
8
9
    println("The width of someResolution is \(someResolution.width)")
    // prints "The width of someResolution is 0"

    println("The width of someVideoMode is \(someVideoMode.resolution.width)")
    // prints "The width of someVideoMode is 0"

    someVideoMode.resolution.width = 1280
    println("The width of someVideoMode is now \(someVideoMode.resolution.width)")
    // prints "The width of someVideoMode is now 1280"
  • Memeberwise Initializers for Structure Types
1
    let vga = Resolution(witdth: 640, height: 480)
  • 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. In fact, all of the basic types in Swift—integers, floating-point numbers, Booleans, strings, arrays and dictionaries—are value types, and are implemented as structures behind the scenes.

1
2
3
4
5
6
7
8
9
10
    enum CompassPoint {
        case North, South, East, West
    }
    var currentDirection = CompassPoint.West
    let rememberedDirection = currentDirection
    currentDirection = .East
    if rememberedDirection == .West {
        println("The remembered direction is still .West")
    }
    // prints "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.

1
2
3
4
5
6
7
8
9
10
11
    let tenEighty = VideoMode()
    tenEighty.resolution = hd
    tenEighty.interlaced = true
    tenEighty.name = "1080i"
    tenEighty.frameRate = 25.0

    let alsoTenEighty = tenEighty
    alsoTenEighty.frameRate = 30.0

    println("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
    // prints "The frameRate property of tenEighty is now 30.0"
  • 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:

1
2
3
4
5
6
7
    // Identical to (===)
    // Not identical to (!==)

    if tenEighty === alsoTenEighty {
        println("tenEighty and alsoTenEighty refer to the same VideoMode instance.")
    }
    // prints "tenEighty and alsoTenEighty refer to the same VideoMode instance."
  • Assignment and Copy Behavior for Strings, Arrays, and Dictionaries

    Swift’s String, Array, and Dictionary types are implemented as structures. This means that 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.

    This behavior is different from NSString, NSArray, and NSDictionary in Foundation, which are implemented as classes, not structures. NSString, NSArray, and NSDictionary instances are always assigned and passed around as a reference to an existing instance, rather than as a copy.

Comments