GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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+

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 more!

Learn more here

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.