I have previously posted about this issue, without success and seemed to have reach a dead-end, ANY ADVISE or Support will be greatly appreciated
Firstly WHAT I’m trying to achieve:
I have a number of Individual Functions setup in a “ScoreCard” model, that includes a (Plus) & (Minus) Button, with the result shown in each (seperate) label - see below
@IBOutlet weak var t1TriesLabel: UILabel!
var num1 = 0
THESE WORK FINE !! (Note there is Two Teams and thus the reference to t1 (team One))
The following is loaded under my ViewController:
_ = tm1Score.text
_ = t1TriesLabel.text
_ = t1ConvLabel.text
_ = t1DropLabel.text
_ = t1PenkLabel.text
_ = t1PentLabel.text
and the following EXTENSION is at the bottom (outside main code (from previous advise received)
extension String {
public func toInt() -> Int? {
if let num = NumberFormatter().number(from: self) {
return num.intValue
} else { return nil }
}
HERE IS THE ISSUE -
I have a SCORE LABEL that needs to reflect the Combined Scores (.text) from all these Sub-Labels, and I cannot get this right…
Hey KoosNZ, it’s really hard to follow the code that you’ve posted, however maybe i can provide an example:
The best thing to do would be to add up two integers and then assign it to the label:
var a = 1
var b = 2
myLabel.text = String(a+b)
Notice that summing two integers results in integer sum so you must case it as a String before assigning to the label’s text property.
In your case, if you want to extract the text properties from two labels and sum it up, you would have to convert them to an Int type before summing, and then cast the sum as a String to assign it back to a label.
var a = Int(myLabel.text)
var b = Int(myLabel2.text)
myLabel3.text = String(a + b)
My code might not be right because I’m just typing off the tip of my tongue, however, the idea hopefully helps you with your specific situation.
Firstly, a huge Thank You for your Tutorials, it continues to be of huge help.
I have been at this single issue now, going into my 3rd day, and have been through the Tutorials more than once, I DID NOT Give myself an easy task with an App that is probably way above my “pay-grade” but this is the last issue to finish my 300 lines plus of code.
Being New, i sort of understand What you refer to, but seems more WHERE to put the code that’s the issue, so I’m working on Trial-&-Error at this point.
I’m not sure if this CODE is ONE Section, or what part of (if any) needs to be under the VC (above override), or at the Button i’m trying to action (t1Score), at the individual Buttons (t1Tries/t1Conv) and/or the bottom.
I will continue to work through the issue, greatly appreciate the support, and keep you updated
@CodeWithChris,
Further to my earlier reply as to “Where” to load this code,
I get the following error wherever I try - “Cannot use instance member ‘t1TriesLabel’ within property initializer; property initializers run before ‘self’ is available”
Please help