Structs everywhere

You saw how the Location struct and a simple Int have much the same copy-on- assignment behavior. This is because they are both value types, and both have value semantics.

You know structs represent values, so what exactly is an Int then? If you were to look at the definition of Int in the Swift library, you might be a bit surprised:

public struct Int : SignedInteger, Comparable, Equatable {

// …

}

That’s right! The Int type is also a struct. In fact, many of the standard Swift types are structs: Double, String, Bool, Array and Dictionary are all defined as structs. As you will learn in future chapters, the value semantics of structs provide many other advantages over their reference type counterparts that make them ideal for representing core Swift types.

results matching ""

    No results matching ""