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

WordScramble score the hard way

Forums > 100 Days of SwiftUI

This works, but I know its a horrible method. Well, maybe not horrible since it works, but its the hard way. My issue is every time I try to add newWord.count to gameScore it gives me issues that I can't add this to an Int which is what gameScore is. I tried INT(newWord) but couldn't...

            gameScore += 3
        }
        if newWord.count == 4 {
            gameScore += 4
        }
        if newWord.count == 5 {
            gameScore += 5
        }
        if newWord.count == 6 {
            gameScore += 6
        }
        if newWord.count == 7 {
            gameScore += 7
        }
        if newWord.count == 8 {
            gameScore += 8
        }
        newWord = ""
    }

3      

You could simplify all of this into 1 line of code if you think about it.

If you look at all of your if closures, they all do basically the same thing.

gameScore += someNumber

But someNumber is not an actual variable in your code. So, what else could you replace someNumber with that will always represent the number that you are trying to add to gameScore?

I'm not sure why you are getting the error you are getting, but I might be able to help more if you can provide the exact error message that you are seeing.

Int(newWord) would not work though, because that is basically trying to convert a word into a number, and the compiler will not know how to do that.

3      

Have you tried var contents = newWord.count then print or add the contents to your gamescore see what happens? Not saying this is right or proper way to do things since I can't see exactly what you're trying to achieve, your full code is not listed, but the print statement should get you in the right direction. If all else fails print everything and anything that you're not sure what it is. Is very handy tool in playground. Right click on go to defenition that always helps too. And most importantly don't forget snacks. Have a nice day!

3      

Not sure what the issue is. Did this in a playground and works fine

let newWord = "driving"

var gameScore = 0

gameScore += newWord.count

print(gameScore) // prints 7

Option+click on your gameScore to check that it an Int as newWord.count is an Int already.

3      

The error is "Left side of mutating operator isn't mutable: 'count' is a get-only property" when I try to add newWord += gameScore. When I option-click gameScore, I get this... @AppStorage var gameScore: Int { get nonmutating set }.

My code does work, but I know its not optimal.

3      

If you are using

newWord += gameScore

You are trying to add an Int to a String. That will not work. Instead, you should be adding the number of letters in the word to the gameScore.

gameScore += newWord.count

If you are trying that in reverse...

newWord.count += gameScore

You are trying to change the value of newWord.count by adding gameScore to it.

That will not work, and is probably the reason it is telling you that the left side is not mutable.

If you have the word "Dog" and get the count property of it, it would be 3. Then, if you try to say "I want to add 5 to that number" it wouldn't make sense to allow you to do that, because the word would still be "Dog" and it would not suddenly have 8 letters in it.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.