When I try to make a simple app, I get this error:
@IBAction func pickButton(sender: UIButton) {
numberLabel.text = String(format: "The number is %@", randomNumber) Thread 1: EXC_BAD_ACCESS (code=1, address=0x176b)
}
Here is the full code:
import UIKit
class ViewController2: UIViewController {
@IBOutlet weak var numberLabel: UILabel!
@IBOutlet weak var pickButton: UIButton!
var randomNumber = arc4random_uniform(10000) + 1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func pickButton(sender: UIButton) {
numberLabel.text = String(format: "The number is %@", randomNumber)
}
}
Please help, I don't know what I should do.
The
%@
format is for printing Foundation objects such asNSString
,NSNumber
etc.is a
UInt32
and that is printed with%u
for "unsigned integer":Alternatively, use
%@
and convert the integer toNSNumber
:But in your case the easiest solution is to use string interpolation: