Sum of UILabel (Text)

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 :+1::+1:

Firstly WHAT I’m trying to achieve:

  1. 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

    @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)” }

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 }

I have a SCORE LABEL that needs to reflect the Combined Scores (.text) from all these Sub-Labels, and I cannot get this right…

@IBOutlet weak var tm1Score: UILabel!


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

Thanks :+1::+1:

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

Afternoon @CodeWithChris
I’ve been at this issue now for 4-days, including revisiting your Tutorials 5-7, without success.

Is there another (Secure) Forum you can suggest where I might be able to find a resolution please…?

@KoosNZ a 2second search on google would have most probably helped you by now.

As a summary, you are trying to set an IBOutlet’s properties outside of a function scope, which you can’t do.

Try moving your score line of code into one of the action functions below an increment.

And just for completeness sake, here is is link to my github where I threw together a quick solution for you.

We sorted the Code through a 3rd Party,
Thank for the advice