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

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

Forums > 100 Days of SwiftUI

@boat  

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

2      

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)

3      

@boat  

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

2      

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.

3      

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.

3      

@boat  

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 ?

2      

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.

3      

@boat  

Very clear now.

Thank you @roosterboy

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.