A Score Card for Golf, using Realm and Swift5

r_a_chalmers

Robert A. Chalmers

Posted on May 5, 2019

A Score Card for Golf, using Realm and Swift5

The story begins here. My first post to Dev.to.
Begun: May 5th, 20129.
Last Edited/Updated: May 8th, 2019

Wherein I am following the stages of development for my Swift + Realm Golf Scorecard.

Part 1.

I'm building a 1 to many database, keeping track of scores on a particular course.

The database objects as I see them so far. This first part is in a Class I've called Game. Here I can have up to 4 players keeping their scores for a game.
So this is the "many" - and this score and player group is for a particular Course. The 1 of the 1-to-many.

Game.swift

  class Game: Object {
  // Date game played and description or name of course
  @objc dynamic var played = Date()
  @objc dynamic var gameDescription = ""
  @objc dynamic var created = Date()
  // names of up to 4 players
  @objc dynamic var nameA = ""
  @objc dynamic var nameB = ""
  @objc dynamic var nameC = ""
  @objc dynamic var nameD = ""
// hole number and details
  @objc dynamic var holeNumber = 0.0
  @objc dynamic var holeScoreA = 0.0
  @objc dynamic var holeScoreB = 0.0
  @objc dynamic var holeScoreC = 0.0
  @objc dynamic var holeScoreD = 0.0

  @objc dynamic var holePar = 0.0
  @objc dynamic var holeDistance = 0.0
  @objc dynamic var latitude = 0.0
  @objc dynamic var longitude = 0.0

  @objc dynamic var coursename: Course!
  }

The Course object is much simpler so far. This is the 1 element of the 1-to-many arrangement.

Course.swift

class Course: Object {
    @objc dynamic var coursename = ""
}

Part 2

Concurrently working on Mapbox maps to go with the project.

Maps at https://studio.mapbox.com
and the Opening tutorial at
https://docs.mapbox.com/help/tutorials/first-steps-ios-sdk/

I started with using the Main.storyboard option to start with, but reverted to the Swift Code version due to problems with XCode errors that I didn't have the patience to solve. Code gives me more control anyway.

In the code on their web page you will note that the Copy elements do not always lign up with the code you need. So copy the whole thing.

The bit of their code that is depreciated in that first section you will see commented in my code below.

In short:
this:

let camera = MGLMapCamera(lookingAtCenter: annotation.coordinate, fromDistance: 4500, pitch: 15, heading: 180)

should be this:

let camera = MGLMapCamera(lookingAtCenter: mapView.centerCoordinate, altitude: 4500, pitch: 15, heading: 180)
//
//  ViewController.swift
//  MapBoxTutoriaCodeVersion
//
//  Created by Robert Chalmers on 08/05/2019.
//  Copyright © 2019 R.A.Chalmers. All rights reserved.
//

import UIKit
import Mapbox

class ViewController: UIViewController, MGLMapViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let url = URL(string: "mapbox://styles/rachalmers/cjvcj22si4bjh1fqugod4sfyh")
        let mapView = MGLMapView(frame: view.bounds, styleURL: url)
        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        mapView.setCenter(CLLocationCoordinate2D(latitude: 52.088122, longitude: 1.284371), zoomLevel: 16.08, animated: false)
        view.addSubview(mapView)
        mapView.styleURL = MGLStyle.satelliteStyleURL
        // Add a point annotation
        let annotation = MGLPointAnnotation()

        annotation.coordinate = CLLocationCoordinate2D(latitude: 52.088122, longitude: 1.284371)
        annotation.title = "Seckford Golf Club"
        annotation.subtitle = "Suffolk's best golf club!"
        mapView.addAnnotation(annotation)

        // Set the map view's delegate

        mapView.delegate = self

        // Allow the map view to display the user's location
        mapView.showsUserLocation = true
    }

    func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
        // Always allow callouts to popup when annotations are tapped.
        return true
    }

    func mapView(_ mapView: MGLMapView, didSelect annotation: MGLAnnotation) {
        //let camera = MGLMapCamera(lookingAtCenter: annotation.coordinate, fromDistance: 4500, pitch: 15, heading: 180)
        //let camera = MGLMapCamera(lookingAtCenter centerCoordinate: annotation.coordinate, fromDistance: 4500, pitch: 15, heading: 180)
        let camera = MGLMapCamera(lookingAtCenter: mapView.centerCoordinate, altitude: 4500, pitch: 15, heading: 180)
        //'init(lookingAtCenter:fromDistance:pitch:heading:)' is deprecated: Use -cameraLookingAtCenterCoordinate:acrossDistance:pitch:heading: or -cameraLookingAtCenterCoordinate:altitude:pitch:heading:.
        mapView.fly(to: camera, withDuration: 4,
                    peakAltitude: 3000, completionHandler: nil)
    }


    }

But otherwise, so far so good.
The first map is here. MapBox Map

💖 💪 🙅 🚩
r_a_chalmers
Robert A. Chalmers

Posted on May 5, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related