Ray Wenderlich First iOS SwiftUI App Course - Part 3

Here we go! Back from Thailand and a couple of days of Half Term left. Man it really makes a difference using the screen and not having to squint! Carrying on with Ray's excellent first Swift with UI App...

Start Time - 11:15

SwiftUI Binding

So at the moment the slider does not work.

Bindings are ways of saying a view will always be tied to a STATE variable. So we will bind together the slider with a state var.

Ok so I have put in the lines of code, thus BINDING together the state var with the slider. Cool!

Strings

String interpolation - know all this.

Variables

Again, real basics here. Casting from one type to another.

Each variable has a certain lifetime. This is to do with scope. Local variables etc.

Swift Basics

I know this will be a lot of recap so more of a skim through...

Swift Standard Library

Stuff about Int random. Point is you do not need to memorise everything. You learn as you go then refer back to bits etc.

Writing Methods

Putting in a method to show what the score is for the round (a nominal 999 at the moment). Ray's advice is to put the methods later on and the state variables at the top.

Challenge! How to calculate the score - how close to get slider to the target number. So in plain English how to work this out.

Now I remember a shortcut here - rather than a complicated if statement, I can use the absolute difference - that doesn't matter whether it is bigger or smaller.

So my way would be this. Score = abs(sliderValue - target). Or vice versa.

Algorithm - a series of steps to solve a computational problem.

If/Else statements

Here is Ray's code



Rather than going over this again, I've created my own much simpler way.....

func pointsForCurrentRound() -> Int {
        var score = 100
        var difference = abs(target - Int(self.sliderValue.rounded()))
        return score - difference

    }

Challenge! Ray is gradually making it simpler and what I have above is ultimately that.

Variables and Constants

One thing with the code above is that the values do not mutate. So making them as constants is better.

Type Inference

Getting rid of where we have explicitly declared the type.

Made code even shorter -

func pointsForCurrentRound() -> Int {
        let difference = abs(target - Int(self.sliderValue.rounded()))
        return 100 - difference

    }

Score is by default 100 so no need for its own variable!

Another interesting point - self not needed! Only needed for the alertIsVisible bit.

Variable Scope

DRY! Easier to read, less prone to errors etc. One thing I disagree with is creating a new function for the rounded value of the slider. Better to keep as it is as I know the more methods that are put in, the harder it is to keep tabs with it all!


And this seems like a good point to stop at!

Finish Time - 12:30 (1 hour 15 minutes)

Lots of good practice with how to use the new SwiftUI format. It's taking time to adapt but honestly, it is all very logical and I like it. Two more entries for this course and it's done!



Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)