SCORE CARD Calculations

I am building an APP to keep Score (Rugby) and stuck on the following issue, if someone could help or direct me where to research please…

In my CODE below, I created TWO Sets of functions,
First a “Plus” button with “Label” to display the number of Tries (with associated Minus correction) as “t1Tries…” - Next the Same Function when a Kick is taken as “t1Conv…”

These work as I need , however I have a Total Score Label “tm1Score” and do not know how to display the sum of the labels, this “(t1TriesLabel)” + “(t1ConvLabel)” ???

@IBOutlet weak var tm1Score: UILabel!
???

@IBOutlet weak var t1TriesLabel: UILabel! 
var num1 = 0

@IBAction func t1TriesPlus(_ sender: UIButton) {
    num1+=5; t1TriesLabel.text = "\(num1)"    }

@IBAction func t1TriesMinus(_ sender: UIButton) {
    num1-=5; t1TriesLabel.text = "\(num1)"    }
   

@IBOutlet weak var t1ConvLabel: UILabel!
var num2 = 0

@IBAction func t1ConvPlus(_ sender: UIButton) {
    num2+=2; t1ConvLabel.text = "\(num2)"    }

@IBAction func t1ConvMinus(_ sender: UIButton) {
    num2-=2 t1ConvLabel.text = "\(num2)"    }

Any help greatly apprecited
@MikaelaCaron…???

I imagine Xcode complains that you can not use an operator on a String? It needs to be first converted to an Int. I use this extension in projects when I run into this problem.

extension String {
        //Converts String to Int
        public func toInt() -> Int? {
            if let num = NumberFormatter().number(from: self) {
                return num.intValue
            } else {
                return nil
            }
        }
}

To call it is fairly simple:

"123".toInt() // 123

That what you are after?

Blessings,
—Mark

Mark @FoundationSW,
Thanks for the reply mate,
I probably should have mentioned that I’m an Ol’ Fella and very new to Coding (lol) so its slow going my side, so excuse the stupid questions…

So I’m a little lost, should it be something like this…?

@IBOutlet weak var t1TriesLabel: UILabel!
extension String { public func toInt() -> Int? {
if let num = NumberFormatter().number(from: self) {
return num.intValue
} else {
return nil
}
}
}

and not sure what you mean by “123”.toInt ()

Thanks anyway

I am in the same boat! Thanks Goodness for Chris’s excellent teaching method, this old dog has learned a trick or two.

The extension is placed at the very end of your code, outside the last bracket.

If I am reading your code correctly, you have a couple labels that you want to add together. So you would do something like this:

let tries = t1TriesLabel.text
let conv = t1ConvLabel.text

tm1Score.text = toInt().tries + toInt().conv

This would add the two labels together and place the sum in the new tm1Score label.

If this isn’t what you are after, let us know, we will take a look.

Blessings,
—Mark

Thanks Mark @FoundationSW
Will give this a go and see where I end up and will let you know.
YES, HUGE Thumbs-Up to Chris & Crew, especially @MikaelaCaron who has also helped.
I’m 51yrs old and my APP is probably Way-Above-My-Paygrade (lol) but the aim is a “functional Referee Assistant” and i’m sooooooo close.
Thanks Again Mate,

Mark @FoundationSW
I’m getting there slowly, but think I need to “Declare/Add” the “labels” first as I have an ERROR "Use of unresolved identifier ‘t1TriesLabel’ (and) ‘t1ConvLabel’

My Extension code (at the bottom now reads:
extension String {
public func toInt() -> Int? {
if let num = NumberFormatter().number(from: self) {
return num.intValue
} else { return nil }
}
}

let t1Tries = t1TriesLabel.text
let t1Conv = t1ConvLabel.text

NOTE that i reference “t1Tries” as opose to just “Tries” as there is 2 teams…
Any suggestions…?

Correct, you need to create an outlet to the ViewController of the labels you are using. I had thought you already did that. Chris shows you how to do that in his lessons, the CNTL drag to the VC.

Sounds like you are making progress. Well done!
Blessings,
—Mark

wait a minute, i see that you are already using global num1 and num2 variable as the values for your label

why not just do sum = num1 + num2 then?

but to answer your question you can convert a string value (or the label text in this case) to int by simply doing [name of your label].text.toInt()

thus to add two label values together you can do

sum = t1TriesLabel.text.toInt() + t1ConvLabel.text.toInt()

but really with how you coded your program you wouldn’t need to convert your label as you already have num1 and num2 ready

Mark @FoundationSW,
I’ve been at it for a few hours and no success yet :pensive:
If I add it to the ViewController , then it gives me more/other errors.
Still trying to resolve, but thanks anyway Mate

@fuerte.francis,
Hey Mate, thanks for your reply.
This is my 1st App and I did not pick an easy one, AND still learning as well.

Firstly, to your response, and those of @FoundationSW, I’m NOT sure if I need a String to do this, but trying to follow and implement what is suggested.

All I’m trying to do (as the Individual portions work) is to have a TOTAL Score Label, so will try yours to see how I get on and will let you guys know…

AN OVERVIEW OF WHAT I’m TRYING TO DO:

  1. It is a RUGBY App, and Scoring is done through a TRY (5pts), Conversion (2pts), Penalty or Drop Kick (3pts) or Penalty Try (7pts)

  2. Ideally I want EACH FUNCTION, such as a TRY to only step in +1, and the SCORE (Sum) to show 5pts. This way the result at the end will show 3x Tries Scored = 15pts - but that I will try and do once I get this issue sorted.

  3. To simplify the “calculation” (as I do not know better at this point) is to make the TRY +5, and the TOTAL SCORE then to “Sum” Tries+Conversions+Kicks, this is my current issue.

It would be AWESUM to go straight to Step3, but cannot even get through Step 2 at this point, so ANY & All Advise is Appreciated,

Thanks to everyone so far :+1::+1:

@fuerte.francis,

Q1 - Where do I add the [name of your label].text.toInt()…?
Is this at each fuction, as in - t1TriesLabel.text.toInt(), but where and as what…?

Q2 - Then the sum = t1TriesLabel.text.toInt() + t1ConvLabel.text.toInt() at the SCORE LABEL, thus:

@IBOutlet weak var tm1Score: UILabel!
sum = t1TriesLabel.text.toInt() + t1ConvLabel.text.toInt()

Did I get anything correct…?

Hey Mark @FoundationSW,

Sorry Mate, I’m completely lost now…

If I create the LABEL as an OUTLET in VC, seems to conflict with the same Label in the function…?

Sorry, I did not want to add to your confusion. With out seeing your code it is hard to provide a clear direction.

If you can post your code, we might be more helpful in clearing up the confusion.

Blessings,
—Mark

Morning Mark @FoundationSW,
Thank you for the ongoing support/advise mate, it is appreciated.
Below the Full Part of the Code, I think is relevant to the issue…

import UIKit
import AVFoundation

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate
{
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var playButton: UIButton!
@IBOutlet weak var pauseButton: UIButton!
@IBOutlet weak var resetButton: UIButton!
@IBOutlet weak var pickerView: UIPickerView!

@IBOutlet weak var gameDetailsField: UITextField!
@IBOutlet weak var gameDetailsView: UITextView!
@IBOutlet weak var gameTick: UIButton!

@IBOutlet weak var team1DetailsField: UITextField!
@IBOutlet weak var team1DetailsView: UITextView!
@IBOutlet weak var team1Tick: UIButton!


@IBOutlet weak var team2DetailsField: UITextField!
@IBOutlet weak var team2DetailsView: UITextView!
@IBOutlet weak var team2Tick: UIButton!


var audioPlayer = AVAudioPlayer()

var timer = Timer()
var isTimerRunning = false
var counter = 10


override func viewDidLoad() {
    super.viewDidLoad()
    
    resetButton.isEnabled = false
    pauseButton.isEnabled = false
    playButton.isEnabled = true
    resetButton.isHidden = true
    pauseButton.isHidden = true
    playButton.isHidden = false
    timerLabel.isHidden = true
    pickerView.isHidden = false
    
    gameDetailsField.delegate = self
    }
    
@IBOutlet weak var tm1Score: UILabel!


    
@IBOutlet weak var t1TriesLabel: UILabel!
var num1 = 0

   
@IBAction func t1TriesPlus(_ sender: UIButton) {
    num1+=1; t1TriesLabel.text = "\(num1)"    }

@IBAction func t1TriesMinus(_ sender: UIButton) {
    num1-=1; t1TriesLabel.text = "\(num1)"    }

    
@IBOutlet weak var t1ConvLabel: UILabel!
var num2 = 0

@IBAction func t1ConvPlus(_ sender: UIButton) {
    num2+=1; t1ConvLabel.text = "\(num2)"    }

@IBAction func t1ConvMinus(_ sender: UIButton) {
    num2-=1; t1ConvLabel.text = "\(num2)"    }


@IBOutlet weak var t1DropLabel: UILabel!
var num3 = 0

@IBAction func t1DropPlus(_ sender: UIButton) {
    num3+=1; t1DropLabel.text = "\(num3)"    }

@IBAction func t1DropMinus(_ sender: UIButton) {
    num3-=1; t1DropLabel.text = "\(num3)"    }


@IBOutlet weak var t1PenkLabel: UILabel!
var num4 = 0

@IBAction func t1PenkPlus(_ sender: UIButton) {
    num4+=1; t1PenkLabel.text = "\(num4)"    }

@IBAction func t1PenkMinus(_ sender: UIButton) {
    num4-=1; t1PenkLabel.text = "\(num4)"    }


@IBOutlet weak var t1PentLabel: UILabel!
var num5 = 0


@IBAction func t1PentPlus(_ sender: UIButton) {
    num5+=1; t1PentLabel.text = "\(num5)"    }

@IBAction func t1PentMinus(_ sender: UIButton) {
    num5-=1; t1PentLabel.text = "\(num5)"    }


@IBOutlet weak var t1YellLabel: UILabel!
var num6 = 0

@IBAction func t1YellPlus(_ sender: UIButton) {
    num6+=1; t1YellLabel.text = "\(num6)"    }

@IBAction func t1YellMinus(_ sender: UIButton) {
    num6-=1; t1YellLabel.text = "\(num6)"    }


@IBOutlet weak var t1RedLabel: UILabel!
var num7 = 0

@IBAction func t1RedPlus(_ sender: UIButton) {
    num7+=1; t1RedLabel.text = "\(num7)"    }

@IBAction func t1RedMinus(_ sender: UIButton) {
    num7-=1; t1RedLabel.text = "\(num7)"    }

extension ViewController : UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true }

}

extension String {
public func toInt() -> Int? {
if let num = NumberFormatter().number(from: self) {
return num.intValue
} else { return nil }
}
}

UPDATE!!

I have been able to “Add” the following to my VC without ERROR
_ = t1TriesLabel.text
_ = t1ConvLabel.text
_ = t1DropLabel.text
_ = t1PenkLabel.text
_ = t1PentLabel.text

So now just working on the SUM CODE I guess…?

Thanks

do you have an outlet for the sum of these 2?

like t1SumLabel?

it can be as simple as

t1SumLabel.text = “((num1+num2))”

@fuerte.francis

I have one In my Code (see above) or do you mean in ViewController…?

My Code above show what i have in the VC and below…?

then it should just be tm1Score.text = “((num1+num2))” then

@fuerte.francis do I add is as a Func or Var…?
Sorry im new mate mate

when do you want this to calculate? automatic?