Build A Simple iPhone App - Part 3

Continuing with the course! So I've got the basic layout features pretty much sorted, complex as they are. It looks like for the next part that I'll create an array and link this to the code. Then this will bring up different facts from the array. It will be good to put this into practice. The more practice, the more it will make sense. That's the idea at least!

Creating a Data Collection

Collections - data types that allow you to create and manage groups of information. An array works best here - we do not need dictionaries! I know the difference between these from before.

@IBAction func showFact() {
        
        let facts = ["Michael Schumacher has won the most F1 races and championships", "Game of Thrones started in 2011", "Hugh Grant starred in About a Boy"]
        
        funFactLabel.text = facts[1]
        
        

    }

I've since had to change the position of this - to appear outside of the function so that it is GLOBAL - not LOCAL! I've also had to play around with the size etc. and ended up creating a new label, which I've renamed. 

Refactoring into a Model

The point that Pasan makes is at present, we'd have to copy and paste a lot of the code e.g. the array into another view controller. Manually correcting each one is not good! Having a single data model makes much more sense! Having errors and fixing them in one place is a lot easier. 

I've created an extra file in the project - the idea of this is that it will be code that can be accessed from the view controller, and any other part of it, rather than the code being copied and pasted etc. 

Structs or Classes

My understanding of this is only using structs for 'simple' data, e,g. shapes or when not much info is needed. Classes are more complex. A good clear example - makes perfect sense!

Model-View-Controller

Design patterns - these are the solutions for avoiding problems. Data from an object goes into either a struct, enum of class. Where the data comes from doesn't matter; the object we create organises the data, performs logic and allows it to basically happen. 

MVC!

Model - does not communicate with the view. It is like the storage in a way. The model can hold the key classes etc.

View - what the user can see. It displays data from the model. These are shown in the main storyboard file. We can have several views across the app.

Controllers - This mediates between the two. Contains code to communicate between the model and the view. The view controller class is the controller we are currently using. Methods like 'view did load', the controller pulls data out of the model. Most apps have several of these. 

OK, so the MVC seems to be crucial. Using the models will be the key part, and not having too much on the controller aspect of it!

Finishing Up Our Model

So our code needs completing! At the moment, nothing happens after the button is tapped once. Basically we need a random number generator. 

In the model...

mport GameKit

struct FactProvider {
    
    let facts = ["Michael Schumacher has won the most F1 races and championships", "Game of Thrones started in 2011", "Hugh Grant starred in About a Boy"]

func randomFact() -> String {
    
    let randomNumber = GKRandomSource.sharedRandom().nextInt(upperBound: facts.count)
    
    return facts[randomNumber]

    }
}

And in the view controller....

newfunFactLabel.text = factProvider.randomFact()

Really clever stuff!

Again, I found the quiz hard - lots of technical stuff. But I am learning from mistakes! Putting this code into a context is definitely helping and is long overdue. It's making me think that after this course, and the error handling one, I'll be done with Treehouse for now, so I can try out some of the Udemy ones. Anyway, looking forward to more tomorrow!




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)