TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Unwrap "create Player struct" challenge

Forums > Swift

Hello everyone,

is there a topic with the solutions for the challenges in the Unwrap app ?

If not I want to submit this challenge here:

Create a Player struct with a score integer property that has a property observer so that whenever the score has changed it prints "The score is now " followed by the new score value.

I wrote this:

struct Player {
    var score: Int {
        didSet {
            print ("The score is now \(score)")
        }
    }
}
var player = Player(score: 5)
player.score = 90

The app, however, returns it to me as wrong, but I don't believe (or I don't see) anything wrong in the code.

3      

@Theo wonders:

The app, however, returns it to me as wrong, but I don't believe (or I don't see) anything wrong in the code.

What might be wrong is you're running this as code in an application?

Instead, try running it in a Swift Playground. I pasted your code into a Playground and it works just fine. Change player.score a few more times for validation!

struct Player {
    var score: Int {
        didSet {
            print ("The score is now \(score)")
        }
    }
}
var player = Player(score: 5)
player.score = 90  // This works great in Playgrounds.
player.score = 42
player.score = 34

Keep coding!

4      

I'm sorry, maybe I didn't explain myself well. In Swift Playground and also in Xcode it works perfectly, but what I meant is that in Paul's app (Unwrap), it doesn't. Maybe there's another way to do it ? Something like a "standard" way ? Or maybe it's just a bug of the application and I'm overthinking it. Anyway I understand that these are beginner questions, thank you for taking the time to respond!

3      

FWIW, Paul has the source code for Unwrap available on his github page and you can always check it to see what the expected answer(s) are.

4      

I hadn't considered that, thank you.

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot 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.