Swift Thread 1:EXC_BREAKPOINT error - ios

I'm a newbie to swift and for now it's a pain in the a$$ to code.
I'm trying to make a very simple app where the user enters a number to a textfield
and then when a button is clicked it shows the number + 1 in a label.
I always get this error:
Thread 1:EXC_BREAKPOINT (code=1, subcode=0x1002c11ec)
This is my code:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var tav: UITextField!
#IBOutlet weak var fogy: UITextField!
#IBOutlet weak var ar: UITextField!
#IBOutlet weak var eredmeny: UILabel!
#IBOutlet weak var szamolasBtn: UIButton!
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 szamol(_ sender: Any) {
let number = Int(tav.text!)! + 1
eredmeny.text = String(describing: number)
}
}

I ran your code and it works fine if an integer in entered in the textfield. But if you enter any character other than a string then the app crashes because Int(tav.text!)! returns nil and adding 1 to nil. Check for nil first and then add something to it.

Related

Geting a fatal "error: unexpectedly found nil while unwrapping an Optional value" when chaging value of label on button press

I keep getting nil value when I press button.
import UIKit
class ViewController: UIViewController {
//MARK: Properties
#IBOutlet weak var txt_vrednost: UITextField!
#IBOutlet weak var btn_dodaj: UIButton!
#IBOutlet weak var lbl_stanje: UILabel!
var novcanik = 0.0
//MARK: Acttion
#IBAction func btn_dodaj_pressed(_ sender: Any) {
let values = Double(txt_vrednost.text!)
novcanik = novcanik + values!
lbl_stanje.text=String(novcanik)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
First of all make sure all your outlets are connected with storyboard. You are using force unwrap everywhere, which is causing crash :
import UIKit
class ViewController: UIViewController {
//MARK: Properties
#IBOutlet weak var txt_vrednost: UITextField!
#IBOutlet weak var btn_dodaj: UIButton!
#IBOutlet weak var lbl_stanje: UILabel!
var novcanik = 0.0
//MARK: Action
#IBAction func btn_dodaj_pressed(_ sender: Any) {
guard let values = txt_vrednost.text as? Double else {
print("not convertible")
return
}
novcanik = novcanik + values
lbl_stanje.text = "\(novcanik)"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}

Add the value of multiple sliders in swift then average them?

Im doing this wrong I but I'm not sure how to get it I could really use some help. I have 3 sliders (I may need more later) and basically they are 1-10 in value kinda for a rating system and i want them averaged to create an average score.. so this is what I have
#IBOutlet weak var speed: UISlider!
#IBAction func speedChange(sender: AnyObject) {
let speedValue = speed.value
}
#IBOutlet weak var body: UISlider!
#IBAction func bodyChange(sender: AnyObject) {
let bodyValue = body.value
}
#IBOutlet weak var details: UISlider!
#IBAction func detailsChange(sender: AnyObject) {
let detailsValue = details.value
}
#IBOutlet weak var score: UILabel!
func final() {
var thescore = (speed.value + body.value + details.value) / 3
score.text = "\(thescore)"
}
Only problem is it doesn't work. the + errors out, so I need to fix that, but even still i think my methodology is completely off.
It worked for me. Are you sure the outlets in the storyboard are linked to your Controller ?
Also, you may want to call your function final every time a slider changed its value, like this :
#IBOutlet weak var sliderA: UISlider!
#IBAction func sliderAchanged(sender: AnyObject) {
fonction()
}
#IBOutlet weak var sliderB: UISlider!
#IBAction func sliderBchanged(sender: AnyObject) {
fonction()
}
#IBOutlet weak var sliderC: UISlider!
#IBAction func sliderCchanged(sender: AnyObject) {
fonction()
}
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
fonction()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func fonction() {
var thescore = (sliderA.value + sliderB.value + sliderC.value ) / 3
label.text = "\(thescore)"
}​
And I added a call to the function in viewDidLoad method to initilaize the label's text.

accessing variables created in the override func viewDidLoad() { super.viewDidLoad() function outside that function in swift

import UIKit
class SecondViewController: UIViewController {
#IBOutlet weak var labelA: UILabel!
#IBOutlet weak var labelB: UILabel!
var dataPassed:String!
var secondDataPassed:String!
var newVar: String!
var newVar2: String!
override func viewDidLoad() {
super.viewDidLoad()
labelA.text = dataPassed
labelB.text = secondDataPassed
newVar = labelA.text
println(newVar)
}
println(newVar) *** I can't access newVar outside override func viewDidLoad() { - Gives "expected declaration" Its driving me crazy!!!***
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Is there any data in dataPassed? If you don't assign any String to your dataPassed variable, and then you assign newVar to dataPassed, newVar will have nothing to print.
Try:
import UIKit
class SecondViewController: UIViewController {
#IBOutlet weak var labelA: UILabel!
#IBOutlet weak var labelB: UILabel!
var dataPassed:String! = "Test."
var secondDataPassed:String!
var newVar: String!
var newVar2: String!
override func viewDidLoad() {
super.viewDidLoad()
labelA.text = dataPassed
labelB.text = secondDataPassed
newVar = labelA.text
println(newVar)
}
Secondly, it appears that you're trying to println again outside of the function. That isn't going to work, because viewDidLoad is essentially the "main method" of your app. You can create other functions that respond to button touches, etc... and run a println there, but because Swift code is executed functionally, the code you're executing has to be inside of a particular function. While you can declare variables, as you have, you can't perform actions such as printing them outside of a function, because then there's no order/method to the madness.
The only place where you can run Swift code on a standalone basis without functions is in a Swift Playground. In XCode you can select File -> New -> Playground to try this out.

Thread 1: SIGABRT

This is my first question (and program in Xcode to be honest) so apologies if I break any formatting rules, etc!
I am having an issue when creating an area calculator (to be used at a later date in a quote calculator) and having looked at every SIGABRT thread I could find, but I have been unable to resolve it.
import UIKit
class ViewController: UIViewController{
#IBOutlet weak var forename: UITextField!
#IBOutlet weak var surname: UITextField!
#IBOutlet weak var email: UITextField!
#IBOutlet weak var widthInput: UITextField!
#IBOutlet weak var lengthInput: UITextField!
#IBOutlet weak var areaOutput: UILabel!
#IBOutlet weak var perimeterOutput: UILabel!
#IBOutlet weak var results: UILabel!
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 calculate(sender: AnyObject) {
let width = NSString(string: widthInput.text).doubleValue
let length = NSString(string: lengthInput.text).doubleValue
// The calculation used to work out the area of a room
var area = width * length
areaOutput.text = "\(area)"
// The calculation used to work out the perimeter of a room
var perimeter = 2 * (length + width)
perimeterOutput.text = "\(perimeter)"
}
Any help would be appreciated.
Thanks,
Marc

'IBOutlet' property has non-optional type 'UIButton'

Here's my code:
import UIKit
class ViewController: UIViewController {
#IBOutlet var button: UIButton
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.
}
}
It's a simple IBOutlet (straight from the Apple developer docs). It gives me the error "'IBOutlet' property has non-optional type 'UIButton'" and I have no idea how to fix it.
It should be like that (in Beta 3 or before):
#IBOutlet var button: UIButton?
IBOutlets must be optionals so place a ? behind the type.
It can also be-
#IBOutlet var button: UIButton!
or
#IBOutlet var weak button: UIButton! (in case you are not doing view unloading)
if you are using XCODE 6 Beta 4
class SecondViewController: UIViewController {
#IBOutlet weak var name: UILabel! = nil
override func viewDidLoad() {
super.viewDidLoad()
name.text="i m a label"
}
}
This code working fine
But when i replace #IBOutlet weak var name: UILabel? it is not working.

Resources