NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Day 15 - Class Deinitializer Random Output

Forums > 100 Days of SwiftUI

Hey All,

I'm working through typing hand-by-hand the Day 15 Swift in 1 hour consolidation and I noticed an abnormality in the console when running through class deinitializers.

class NewUser {
    let id: Int

    init(id: Int) { = id
        print("User \(id): I'm alive!")
    deinit {
        print("User \(id): I'm dead!")

for i in 1 ... 3 {
    let user = NewUser(id: i)
    print("User \(id): I'm in control!")

for some reason, when this runs the consol spits out a a random user ID for each loop. the id changes each time. anyone have any idea why that's getting returned?

User 1: I'm alive!
User 247: I'm in control!
User 1: I'm dead!
User 2: I'm alive!
User 247: I'm in control!
User 2: I'm dead!
User 3: I'm alive!
User 247: I'm in control!
User 3: I'm dead!


print("User \(id): I'm in control!")

id doesn't belong to any instance of NewUser, so it doesn't correspond to the id property of user.

You need to do this instead to get the result you are expecting:

print("User \( I'm in control!")


Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

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

Reply to this topic…

You need to create an account or log in to reply.

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.