Updating a integer as a label in swift
Updating a integer as a label in swift
I want to change the number value of a label by pressing a button. I used to get errors saying you can't put a number in a string, so, I did string(variable). It now displays the basic number, 1, but when I click it, it doesn't update!
Here is how I set up the variable:
First, I set up the button, to IBAction. Here is the code inside of it:
@IBOutlet weak var NumberOfExploits: UILabel!
@IBAction func exploiter(sender: AnyObject) {
var hacks = 0
hacks += 1
NumberOfExploits.text = String(hacks)
}
Can someone help be find out how to get it to change numbers?
3 Answers
3
First:
let is used for constants, these can not be changed.var is used for variables.
let
var
Second:
Doing plus one is done with += 1 or with = myVariable + 1
+= 1
= myVariable + 1
var myVariable = 1
myVariable += 1
label.text = string(myVariable)
It is important to understand that your variables only live as long as the enclosing class or function is alive. So when you declare a variable, constant inside a function (using var let is a declaration) it will be be gone when the function is complete. A UIViewcontroller is there for as long as you want it to be. So declare things in there that you will need later.
var
let
UIViewcontroller
import UIKit
// things that live here are at the "global level"
// this is a viewcontroller. It is a page in your app.
class ViewController: UIViewController {
// things that live here are at the "class level"
var myClassCounter : Int = 0
// button in interfacebuilder that is connected through an outlet.
@IBOutlet weak var myButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// this is a function that gets called when the view is loaded
}
override func viewDidAppear(animated: Bool) {
// this is a function that gets called when the view appeared
}
// button function from interface builder
@IBAction func myButtonIsTouched(sender: AnyObject) {
// this will be destroyed when the function is done
var myFunctionCounter = 0
myFunctionCounter += 1
// this will be stored at class level
myClassCounter += 1
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Thank you so much!
– benpete420
Aug 29 '15 at 17:21
np. don't forget to accept the answer ;)
– R Menke
Aug 29 '15 at 17:22
If the initial assignment of the variable is in the IBAction, as the OP says, the value will only ever be 2. The variable will need to be declared at class level
– Rich Tolley
Aug 29 '15 at 17:23
Was leaving some room for discovery
– R Menke
Aug 29 '15 at 17:25
I did declare it at class level, still doesn't work. Just goes to 2 Help?
– benpete420
Aug 29 '15 at 17:26
You can also just do
label.text = "\(variable)"
which will display the number as string.
Most important declare the variable within the class but outside any function
var variable = 1
then write the IBAction function this way
IBAction
@IBAction func pressButton(sender : UIButton)
{
label.text = "(++variable)"
}
The syntax ++variable increments the variable by one.
The syntax "(v)" creates a string of anything which is string representable
++variable
"(v)"
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
variable += 1 except second line?
– SwiftStudier
Aug 29 '15 at 17:20