Chapter 10: Structures

大綱

Introducing structures

  • Structures are one of the named types in Swift that allow you to encapsulate related properties and behaviors.

  • Swift automatically provides initializers for structures with all the properties in the parameter list

    • Initializers enforce that all properties are set before you start using them. This is one of the key safety features of Swift.

struct Location {
  let x: Int
  let y: Int
}

struct DeliveryArea: CustomStringConvertible {
  // sturct中可以再有個sturct
  let center: Location
  var radius: Double

  var description: String {
    return """
      Area with center: x: \(center.x) - y: \(center.y),
      radius: \(radius)
      """
  }

  // 定義method
  func contains(_ location: Location) -> Bool {
    let distanceFromCenter = distance(from: (center.x, center.y), to: (location.x, location.y))
    return distanceFromCenter < radius
  }
}

// Swift自動產生的Initializers, 會強制所有變數都有初始值
let storeLocation = Location(x: 2, y: 4)
var storeArea = DeliveryArea(center: storeLocation, radius: 4)

Accessing members

  • you use dot syntax to access members

print(storeArea.radius) // 4.0
print(storeArea.center.x) // 2
  • That code causes the compiler to emit an error. Change fixedArea from a let constant to a var variable to make it mutable

let fixedArea = DeliveryArea(center: storeLocation, radius: 4)

// Error: change let to var above to make it mutable.
fixedArea.radius = 250

Introducing methods

  • defines a function contains, which is now a member of DeliveryArea. Functions that are members of types are called methods.

let area = DeliveryArea(center: Location(x: 5, y: 5), radius: 4.5)
let customerLocation = Location(x: 2, y: 2)
area.contains(customerLocation) // true

Structures as values

  • A value type is a type whose instances are copied on assignment

var area1 = DeliveryArea(center: Location(x: 2, y: 4), radius: 2.5)
var area2 = area1
print(area1.radius) // 2.5
print(area2.radius) // 2.5

area1.radius = 4
print(area1.radius) // 4.0
print(area2.radius) // 2.5

Structures everywhere

  • many of the standard Swift types are defined as structures, such as: Double, String, Bool, Array and Dictionary.

Conforming to a protocol

  • Any named type can use protocols to extend its behavior. In this case, you conformed your structure to a protocol defined in the Swift standard library.

struct DeliveryArea: CustomStringConvertible {
  var description: String {
    return """
      Area with center: x: \(center.x) - y: \(center.y),
      radius: \(radius)
      """
  }
}

Key points

  • Structures are named types you can define and use in your code.

  • Structures are value types, which means their values are copied on assignment.

  • You use dot syntax to access the members of named types such as structures.

  • Named types can have their own variables and functions, which are called properties and methods.

  • Conforming to a protocol requires implementing the properties and methods required by that protocol.

    Where to go from here?

Last updated

Was this helpful?