So the plan is to complete this app today - the Quizzler! Like I've said before, this template could easily be adapted. Once it's done, I could use it for F1 questions, GoT...anything! So being able to understand each step, as well as the object oriented programming being clarified, there have been lots of positives. Here we go!
Init the First Question
letfirstQuestion = allQuestions.list[0]
I figured out that this must be the 0 index with that format. Makes sense!
uestionLabel.text = firstQuestion.questionText
Having the class makes a huge difference to the simplicity of the code!
OK, the tag system makes a lot fo sense. Grouping together several buttons and putting in the tags of 1, 2 etc. That would have been useful for the Yahtzee buttons for each option to click. Anyway, that's something to play around with another time.
func checkAnswer() {
let correctAnswer = allQuestions.list[0].answer
if correctAnswer == pickedAnswer {
print("You got it!")
} else {
print("Wrong!")
}
}
How to Progress Through the Question Bank
var questionNumber: Int = 0
So this is a great way of keeping track of something, whether it's the level one is on, the score, or in this case - the question number!
Using += 1 - that is the best way of adding on one! Not ++. Swift removed the ++ for some reason. But never mind!
OK - challenge was to try to figure out what to do when the questions were up - I failed. Tried a few things but they did not work.
Using the Xcode Debug Console
Right, so using the debug console, using the debug area is important here. You can print the question number here, to establish the value. This is 13. OK so I was actually close, just some of the order of things. Encouraging!
How to Implement UIAlertController
Here is how it looks:
Whenever you see the word 'in', you should also use the word 'self'. Not for for-in loops though!
This is linked to CLOSURES. We'll come back to this apparently.
let alert = UIAlertController(title: "Complete!", message: "You have finished all of the questions. Do you want to play again?", preferredStyle: .alert)
This is REALLY useful stuff! It seems of huge importance to have these action buttons in the code - especially for restarting etc. The code is complex so I'm not going to worry about memorising the syntax!
I'm still going for more complex solutions rather than simpler ones - repeating myself. Remember DRY!
High Level Overview
This was all fine.
Track Progress and Score
I haven't typed anything here but lots of logic and problem solving - very rewarding!
UPDATING score in the code so it resets to 0 every time - already done my friend!
Incorporating Objective C code into Swift
OK, so this is to get pop ups to show if they got an answer right or wrong!
We're going to use a code library - we can access this for free basically.
HUD - Head Up Display
So I've copied over the files - no problem. I've included the 'bridging' option too.
I've also created an alteration - randomised messages for the correct/wrong answers! I posted this in the questions as am proud of it!
Well that is all for now. Lots learned but gotta go!
Here we go! Haven't had the chance over the past two days so am going to do a good half an hour or so now! Last time, it was a big focus on initialisers. I've made of sense of those and hopefully I'll be able to apply that. Now we're moving on to something else - can't remember. Anyway, let's go! Value Semantics Value types - structs, enums etc. struct Point { var x: Double var y: Double } var p1 = Point (x: 1 , y: 2 ) var p2 = p1 If we change the x value of p1, then let's see what happens to p2. It does not change! A value type is COPIED ON ASSIGNMENT! We are copying the underlying value. let p3 = Point (x: 2 , y: 4 ) Here, this constant is IMMUTABLE. You cannot then change the values! However with var (p1 or p2) you can change values, as long as the stored properties are variables (which they are!) struct AnotherPoint { let x: Doubl...
So, once again I've managed to leave a huge gap between these blogs! The good news is that the Summer Holidays are approaching and there will DEFINITELY be time to code then! So, between now and then, I need regular entries so I can learn what is left in 'Learn to Code in Swift 4', then make a new plan for how to take my knowledge forward. Anyway, the next chapter is all about 'Unit Testing', which sounds interesting! The three facets of a successful app are: well designed, stable and easily adaptable to change. Apparently, many app developers do not test their code properly; the most effective approach is to test one line at a time, after writing. That makes sense! So 'Unit Testing' is a method to test individual 'units' - usually methods. Having read over the next few pages, a lot of this seems unnecessary at present; I'm still getting used to writing codes let alone testing and debugging! This is something that I can revisit when I have ha...
Yes - one day's break, but not because I needed or wanted one! I was genuinely to busy for any Swift yesterday, so it feels good to be back! Having written entries each day for a week, I missed it! Enumerations are done for now; Optionals are next... The Absence of Data I think the idea of this is 'nil' - nothing! Pasan explains 'safe' code, where crashes need to be avoided. Optionals are new - since the inception of Swift I believe. struct Person { let firstName: String let middleName: String ? let lastName: String } I've pre-empted the ? - to make middleName an optional. If I didn't have this, then an error would be caused - you can't input nil. The question mark - making it an optional - means that nil CAN be used. So it means it may have a value OR may be nil. They do seem cumbersome, but apparently they are powerful and useful! In the example above, it means that it is a string and ma...
Comments
Post a Comment