WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Question : Please see my two simple sets of Structure. Which one is better ?

Forums > 100 Days of SwiftUI

Hello,

I've been practicing. Please see my two sets of codes. Both are structures for creating Player, and both could work.

It seems Code 2 is neater ? ( is it common to use closure in a property ?)

Code 1


struct Player {
    let name: String
    let number: Int

    init (name:String){
        self.name = name
        number = Int.random(in: 1...99)
    }
}

let playerA = Player(name: "Paul")
print(playerA.number)

Code 2


struct Player {
    let name: String
    var number: Int { Int.random(in: 1...99)}
}

let playerA = Player(name:"Paul")
print(playerA.number)

Thank you in advance,

Boat

   

The two structs are not quite the same.

In the second struct, number is a computed variable that will be recalculated every time it is accessed.

Adjust your test code to this and then try it with each of your Player structs and you will see what I mean:

let playerA = Player(name:"Paul")
print(playerA.number)
print(playerA.number)
print(playerA.number)
print(playerA.number)
print(playerA.number)
print(playerA.number)

1      

Oh

@roosterboy, i just tested the code above, I got your point:

in code 2, it is computed property, so the created instance playerA won't have a fixed number, right?

but in code 1, once the instance is created, playerA will have a fixed number, right ?

Boat

   

in code 2, it is computed property, so the created instance playerA won't have a fixed number, right?

but in code 1, once the instance is created, playerA will have a fixed number, right ?

Yup.

1      

Forgot to mention...

This achieves what you were probably trying to do with the second Player struct:

struct Player {
    let name: String
    let number = Int.random(in: 1...99)
}

You don't need an init since the default memberwise init provided by Swift will give you all you need.

1      

Hm...

@roosterboy, in your code let number = Int.random(in: 1...99),

Even if I change let to var, the value assigned to number from Int.random will still remain the same for the instance, right ? It won't generate a new random value everytime the instance is called.

Right ?

   

Correct, because it is assigned when the struct is created. If you use a var instead of a let, you'll be able to change the value later but until/unless you do so, the property will maintain its initial value.

1      

Very clear now.

Thank you @roosterboy

   

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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.