Learn Courses My Dashboard

How to implement MVVM in Project

Hi Guys,

Alongside the CWC lessons, I’ve been working on a personal side project.

I currently have this hosted on GitHub on: GitHub - dannywade6/GolfStablefordCalculator: This is a personal project to solidify my understanding of the Swift programming language and to build a SwiftUI application with plans of publishing this to the App Store. Stableford is a scoring system in golf which, rather than counting the total number of strokes taken, involves scoring points based on the number of strokes taken at each hole.

I have the StablefordCalculatorView.swift working how I would like it to, however I would like to split this into multiple views using MVVM.

Could I get some advice on how to do this as currently I’m struggling to figure this out.

Any help is greatly appreciated. Thanks.


Hi Danny,

Welcome to the community.

The first thing I would suggest is to draw a diagram of the screens that you want to present to the user and the data that you want to present in each screen.

As a non golfer (well I have walked a golf course and swatted at the ball goodness knows how many times) I was thinking about the kind of data you would store for a given hole and whether you would extend that to all the holes on the course and maybe even other golf courses.

Your model is currently:

struct StablefordCalculator {

    var handicap: Int
    var par: Int
    var strokeIndex: Int
    var gross: Int

    func calculatePoints() -> Int {


Apart from handicap and par, what is the purpose of strokeIndex and gross?

Hi Chris,

Thanks for taking the time to look at the project and for your response.

The app’s function is to work out Stableford scoring which is a type of way to score points for each hole you play. The points you score on the hole are dependent on the Stroke Index and Gross(Strokes Played). In the MarkDown file I have on GitHub details how they are calculated.

I’ll draw out a diagram as that will help me visually work out what I’m trying to achieve.

The StablefordCalculatorView.swift view calculates the point correctly, I’m now stuck with how to spilt this to use MVVM.


If I understand this correctly this is something that you would analyse after you have played a Round. By that I mean you would grab your scorecard and enter all the details for each hole you played and how many strokes you played for each hole and this App would quickly calculate the total points for that Round.

Yeah exactly that! Once the round is complete you would enter the details for every hole and it will calculate your score for the round.

OK so you need to store data for each hole as well as the course played

My thoughts are that your model required to represent a Round on a course might be something like this (I’m just guessing here):

struct Round: Identifiable {
    var id = UUID()
    var course: String
    var date: String
    var points: Int
    var holes: [Hole]

struct Hole: Identifiable {
    var id = UUID()
    var holeNumber: Int
    var handicap: Int
    var par: Int
    var strokeIndex: Int
    var gross: Int

So each time you play a Round you would create a new instance and then populate the data for each hole in that round. At the conclusion of that data entry you would save the data which would calculate the points for that Round.

The opening screen would show a list of the Rounds played and you would select a Round and show a screen with the all the data for each hole.

Adding a Round would probably be in a separate screen and that may branch to another to enter the hole data for the 18 holes (or 9 holes only if that is appropriate).

Is that sort of reasonable?

Thanks Chris, that is exactly how I would like the app to behave.

The model contains all the data I need for now, I will add that to the project and get working on the ViewModel.

I will put the logic I currently have in my View in the ViewModel and attempt to get this working.