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

SOLVED: @day8: structs ... "before being initialized"

Forums > 100 Days of Swift

the example below is based on one of the test-cases..... using //B in the struct definition //A ... would be my favorite - appears logical for me //B & //B1 ... complains "Variable eventTicket used before being initialized" .... well - ticketPrices has values ?!?!?! //B & //B2 ... works .... " Event()" ... why?

struct Event { //A var ticketPrices: [Int] //B var ticketPrices = [100,200,300] func buy(type: Int) { let cost = ticketPrices[type] print("That'll be $(cost), please") } } //A var eventTicket = Event(ticketPrices: [100,200,300]) //B1 var eventTicket : Event //B2 var eventTicket = Event() eventTicket.buy(type: 1) print (eventTicket.ticketPrices.count)

3      

Can you fix your post so it's easier to read the code? It's a little hard to see what you're talking about with the code all run together in a paragraph.

To format a code block, place three backticks ``` on the line before your code and three backticks ``` on the line after your code so that it will be formatted properly. You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.

This makes it far easier to read and also makes it easier for other posters to copy/paste the code in order to test solutions and such.

Doing this will ensure that you end up with something like this:

func printSomething(_ thing: String?) {
    if let thing = thing {
        print(thing)
    } else {
        print("nothing there")
    }
}

instead of this:

func printSomething(_ thing: String?) { if let thing = thing { print(thing) } else { print("nothing there") } }

3      

... this is a rewriting of the original post ...

the example below is based on one of the test-cases..... the ticketPrice Array filled inside the struct definition Using: //1 ... complains "Variable eventTicket used before being initialized" .... well - ticketPrices has values ?!?!?! //2 ... works .... " Event()" ... why?

struct Event {
    var ticketPrices = [100,200,300]
    func buy(type: Int) {
        let cost = ticketPrices[type]
        print("That'll be $\(cost), please")
    }
}
//1 var eventTicket : Event
//2 var eventTicket = Event()
eventTicket.buy(type: 1)
print (eventTicket.ticketPrices.count)

This is another version .... ticketPrices just defined as array of Int Is this not the cleaner why? Specially as the filled array seems not satisfying the "initialization".

struct Event {
    var ticketPrices: [Int]
    func buy(type: Int) {
        let cost = ticketPrices[type]
        print("That'll be $\(cost), please")
    }
}
var eventTicket = Event(ticketPrices: [100,200,300])
eventTicket.buy(type: 1)
print (eventTicket.ticketPrices.count)

3      

Sorry .... just learned more in day9 "memberwise initializers"

One thing is confusing here ... it is said that structs can be seen like types. So I tried the //1 syntax The //2 syntax appears more like calling a function

struct Event {
    var ticketPrices = [100,200,300]
    func buy(type: Int) {
        let cost = ticketPrices[type]
        print("That'll be $\(cost), please")
    }
}
//1 var eventTicket : Event
//2 var eventTicket = Event()
eventTicket.buy(type: 1)
print (eventTicket.ticketPrices.count)

3      

//1 var eventTicket : Event

This gives you that error because Event does not initialize your struct. var eventTicket: Event is just a type annotation. It's essentially telling the compiler "Hey, eventTicket will be a variable of type Event" but doesn't do anything more than that. var eventTicket = Event() gives you an initialized type that you can then use in your code.

Your second example is another way of getting an initialized Event struct that you can use, but it is slightly different.

Your first example gives you the flexibility of having an Event struct that comes with default values. So you can either initialize it with the default ticket prices like this: var eventTicket = Event() or you can give it custom values like so: var eventTicket = Event(ticketPrices: [80, 160, 340])

With your second example, you always have to provide the list of ticketPrices when you initialize an Event.

3      

The //2 syntax appears more like calling a function

Well, that's because you are calling a function.

This code:

var eventTicket = Event()

is really a shorthand for:

var eventTicket = Event.init()

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.