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)” ???
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
}
}
}
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
}
}
}
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
Mark @FoundationSW,
I’ve been at it for a few hours and no success yet
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…
It is a RUGBY App, and Scoring is done through a TRY (5pts), Conversion (2pts), Penalty or Drop Kick (3pts) or Penalty Try (7pts)
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.
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,
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!
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