Your First iOS & SwiftUI App: An App from Scratch

Jan 11 2022 · Swift 5.5, iOS 15, Xcode 13

Part 2: SwiftUI Data

18. Create a Model

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 17. Intro to App Architecture Next episode: 19. Conclusion
Transcript: 18. Create a Model

OK, let’s learn how we can put the theory of App Architecture into practice, by creating a simple data model for Bullseye.

To review, remember that we need three things: the target the user is aiming for, we need the current round, and we need the total score across all rounds. These will all be integer numbers.

OK - let’s open up Xcode and implement the Game struct.

Create group for Models.

Create group for Views. Drag ContentView.wwift into there.

Create group for App. Drag everything else into there.

Build and show error. Go to Project Settings\Build Settings\Info.plist and upstea to Bullseye/App/Info.plist. Build and run to show all is OK.

Create new file iOS\Swift file. Name it Game.swift.

Update to:

struct Game {
  var target: Int = 42
  var score: Int = 0
  var round: Int = 1
}

Add to ContentView.swift:

@State private var game: Game = Game()

Update target label:

Text(String(game.target))

Remember that every object you create can have data and methods. We’ve figured out the three pieces of data we need for Bullseye, but what methods do we need?

To start, for we just would like a single method, that calculates the points for a given guess. So we could call this method points, and have it take a single parameter which is the slider’s value.

Thsi is the first time we’ve written our own method in Swift, so let me take a moment to explain the synax before we dive in.

Imagine you want to add a method that has no input - in other words, no parameters - but does have an output - in other words, a return value.

To do this, you’d simply add a block of code that looks like this before the final curly brace of your class.

Basically you use the keyword func, then you give your method a name, and then you enter an open and closed parenthesis.

In-between the parenthesis, you put any parameters, or input, that you want your method to accept. The format for this is the parameter name, a colon, then the type of the parameter.

You can create more than one parameter if you’d like; you can just separate them by commas. Or you can have methods that take no parameters - in that case you’d just have an open and close parenthesis with nothing inside.

After your list of parameters, you put a dash and a greater than sign, which looks kinda like an arrow. Then you indicate the type of object that you will return as the output of your method, like Int, Double, or Bool.

Inside the curly braces, you put all of your code. When you’re ready to return the output value, you enter the keyword return, then the value you want to return, like 999, 9.14, or true.

Now that we know what we need, we just need to code it up. So let’s switch back to Xcode!

Add method to Game.swift:

func points(sliderValue: Int) -> Int {
  return 999
}

Update alert text:

return Alert(
  title: Text("Hello there!"),
  message: Text("The slider's value is \(roundedValue).\n" +
    "You scored \(self.game.points(sliderValue: roundedValue)) points this round."),
  dismissButton: .default(Text("Awesome!")))

Build and run. Show that the label shows 42, and show that is shows 999 for the points.