UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Question: Why do we need init(){} in Structs at all ? can't we just assign property values for every new instances when we are creating them ?

Forums > 100 Days of SwiftUI

@boat  

I've been reading the formal book of swift.

https://docs.swift.org/swift-book/LanguageGuide/Initialization.html

according to the document

The following two snippets are the same

struct Fahrenheit {
    var temperature: Double
    init() {
        temperature = 32.0
    }
}
var f = Fahrenheit()
struct Fahrenheit {
    var temperature = 32.0
}

var f = Fahrenheit()

What's the whole point of initializer ?

I could just make a new instance everytime by using let or var and then call the struct and give each property a value, isn't this good enough ?

2      

Most initializers are actually going to take parameters, allowing you to set different values for the struct's properties.

On the page you linked to, this is covered by the very next section: Customizing Initialization. Keep reading.

3      

Day 10: Structs

If you go back to Day 10 of your 100 Days of SwiftUI journey, you'll recall the introduction to Structs.

@twostraws introduces the concept of memberwise initializers. I admit it. This is not the most friendly description to toss on a learner just 10 days into a course. This is a mouthful of jargon.

But Lecture 10 should answer your question. Structs, by default, can be initialized by just providing values for each public parameter defined in your struct! You get a free initializer by default. Please convince yourself that private variables (i.e. @State private var) are NOT included in the default initializer. Also, convince yourself that computed properties are not available in the default initializer either.

But as @rooster notes, as you move along your SwiftUI journey, you'll find the need to have a more clever way to initialize some of your structs. In this case, you'll want to craft your own initializer. Indeed, you may want to craft one or more initializers for a Struct.

I agree with comrade @rooster here. Please enjoy the journey! Keep reading!

3      

If you don't provide default values in your struct then you need a way to enter those values when you create an instance of the struct.

struct Person {
    var firstName: String
    var middleName: String
    var lastName: String
    let ethnicity: String
    var height: Int
    var weight: Int
}
let me = Person(firstName: <#T##String#>, middleName: <#T##String#>, lastName: <#T##String#>, ethnicity: <#T##String#>, height: <#T##Int#>, weight: <#T##Int#>)

3      

@boat  

thank you guys.

Didn't expect to have so many answers on the Christmas Day !

Happy Holidays everyone

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.