0Pricing
Swift Academy · Lesson

Memberwise Initializers and Custom Inits

Understanding auto-generated memberwise inits and writing custom initializers.

Memberwise Initializers and Custom Inits is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

Swift structs get a free *memberwise initialiser* that accepts all stored properties. But you can customise or extend this with your own initialiser logic.

Auto-generated Memberwise Init

```swift struct Point { var x: Double var y: Double } let p = Point(x: 3.0, y: 4.0) // auto-generated init ``` No need to write `init` yourself — the compiler provides one.

Adding a Custom Init

```swift struct Circle { var radius: Double var area: Double init(radius: Double) { self.radius = radius self.area = .pi * radius * radius } } let c = Circle(radius: 5.0) ```

Preserving Memberwise Init

Defining a custom init suppresses the auto-generated memberwise init. To keep both, put the custom init in an extension: ```swift struct Rect { var width: Double; var height: Double } extension Rect { init(square side: Double) { self.init(width: side, height: side) } } // Both Rect(width:height:) and Rect(square:) available ```

Default Property Values

```swift struct Config { var debug = false var maxRetries = 3 var timeout: TimeInterval = 30 } let c1 = Config() // all defaults let c2 = Config(debug: true) // memberwise with overrides ```

Initialiser Delegation with self.init

```swift struct Temperature { var celsius: Double init(celsius: Double) { self.celsius = celsius } init(fahrenheit: Double) { self.init(celsius: (fahrenheit - 32) / 1.8) } init(kelvin: Double) { self.init(celsius: kelvin - 273.15) } } ```

Failable Initialisers

```swift struct Percentage { let value: Double init?(_ value: Double) { guard (0...100).contains(value) else { return nil } self.value = value } } let p = Percentage(105) // nil ```

Init with Validation

```swift struct Username { let value: String init?(_ raw: String) { let trimmed = raw.trimmingCharacters(in: .whitespaces) guard trimmed.count >= 3 else { return nil } self.value = trimmed } } ```

Struct Init vs Class Init

Key differences from class initialisers: • Structs can use `self.init()` delegation • Structs don't inherit inits (no `super.init()`) • All stored properties must be initialised before use • No `required` or `designated` / `convenience` keywords

init(from decoder:) Preview

Structs conforming to `Decodable` get an automatic init: ```swift struct User: Decodable { let name: String let age: Int } // Auto-synthesised init(from:) for JSON decoding let user = try JSONDecoder().decode(User.self, from: json) ```

Quick Check

Where should you put a custom init to preserve the auto-generated memberwise init?

Recap

Key takeaways: • Structs get a free memberwise init from the compiler • Custom inits in the struct body suppress the memberwise init • Place custom inits in extensions to keep both • Use `self.init()` to delegate between initialisers • Failable inits return `nil` on invalid input Next: stored properties and property observers.

Frequently asked questions

Is the “Memberwise Initializers and Custom Inits” lesson free?

Yes — the full text of “Memberwise Initializers and Custom Inits” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Memberwise Initializers and Custom Inits”?

Understanding auto-generated memberwise inits and writing custom initializers. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Memberwise Initializers and Custom Inits” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Memberwise Initializers and Custom Inits
  2. Stored Properties and Property Observers
  3. Computed Properties in Structs
  4. Mutating Methods and Copy-on-Write Semantics
← Back to Swift Academy