03 Instance & Lifetime

4 阅读1分钟

Instance & Lifetime

Core Idea

An Instance is a concrete value created from a Type.

Type
 ↓
Instance

Example:

struct Person {
    let name: String
}

let person = Person(name: "Tom")

Person → Type

person → Instance

Lifecycle

Conceptually:

creation
   ↓
initialization
   ↓
use
   ↓
lifetime
   ↓
destruction / release

Initializer

An initializer establishes a valid initial state for an instance.

Person(name: "Tom")

Class Lifetime

For class instances, lifetime is related to reference ownership and ARC.

references
   ↓
instance lifetime
   ↓
deallocation

Important

Instance ≠ Type

Instance ≠ Memory Address

Instance ≠ "everything stored in memory"

An instance may contain stored state, while behavior and type information belong to other implementation layers.

Related