Can't resolve error: Thread 1: signal SIGABRT in swift XCODE - iphone

I have the common error - "Thread 1: signal SIGABRT". Although I can't resolve this. I have researched more about this error, I found stuff like: This error only happens when you are calling on an outlet that doesn't exist. I've looked back into my code looking for some button, label, image or anything that I do not want, I just can't find any trace of the bug! If anyone can spot a bug or anything, that would be great. Thanks!
Here is my code
import UIKit
class ViewController: UIViewController {
//Useful Variables
var cashCount:Int = 0
var cashIncome:Int = 1
//The cash displaying Outlets
#IBOutlet weak var errorDisplay: UILabel!
#IBOutlet weak var cashDisplayLabel: UILabel!
#IBOutlet weak var cashIncomeDisplayer: UILabel!
//The other outlets
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.
}
//When the cash is clicked call this func
#IBAction func CashClicked(_ sender: Any) {
cashCount += cashIncome
cashDisplayLabel.text = "Cash: $\(cashCount)"
}
//The store functions
#IBAction func BuyFoodFUNC(_ sender: Any) {
if cashCount >= 10 {
errorDisplay.text = "Success!"
cashCount -= 10
} else {
errorDisplay.text = "Sorry, Not Enough Funds!"
}
}
#IBAction func BuySubFUNC(_ sender: Any) {
if cashCount >= 20 {
errorDisplay.text = "Success!"
cashCount -= 20
} else {
errorDisplay.text = "Sorry, Not Enough Funds!"
}
}
#IBAction func BuyCarFUNC(_ sender: Any) {
if cashCount >= 800 {
errorDisplay.text = "Success!"
cashCount -= 800
} else {
errorDisplay.text = "Sorry, Not Enough Funds!"
}
}
#IBAction func BuyRVFUNC(_ sender: Any) {
if cashCount >= 1000 {
errorDisplay.text = "Success!"
cashCount -= 1000
} else {
errorDisplay.text = "Sorry, Not Enough Funds!"
}
}
#IBAction func BuyLapTopFUNC(_ sender: Any) {
if cashCount >= 900 {
errorDisplay.text = "Success!"
cashCount -= 900
} else {
errorDisplay.text = "Sorry, Not Enough Funds!"
}
}
#IBAction func BuyPhoneFUNC(_ sender: Any) {
if cashCount >= 700 {
errorDisplay.text = "Success!"
cashCount -= 700
} else {
errorDisplay.text = "Sorry, Not Enough Funds!"
}
}
#IBAction func UpgradeCashINCOME(_ sender: Any) {
if cashCount >= 10 {
errorDisplay.text = "Success!"
cashCount -= 10
cashIncome += 1
cashIncomeDisplayer.text = "Income: $\(cashIncome)"
} else {
errorDisplay.text = "Sorry, Not Enough Funds!"
}
}
}
Here is a screenshot
https://imgur.com/a/Cvh0I

Open up assistant editor (2 circles thing on top right). Open the storyboard on the left screen and the code for the view controller that is causing the error on the right side. Make sure all the dots are bubbled in (dots appear next to where you connect your outlets) on the code side of your screen. If it isn't filled in, drag the bubble to the correct outlet
Also, if you changed the name of an outlet, you will have something like the image below. Press the x to delete that connection and drag the circle to the right of it and reconnect it.

Related

Is there a way to call a function every 5 times a button is pressed (Swift)?

I want to call a function with every five button presses. I currently have the function called on a timer, but it is not a great experience.
Not sure where to start. Here is my current code:
override func viewDidLoad() {
super.viewDidLoad()
#IBAction func nextButtonPressed(_ sender: Any) {
}
func displayMasteredWords() {
masteredWordsView.isHidden = false
masteredWordsView.text = "You just mastered:\(readString)"
}
You can keep the count of the total calls. Every time this number is divisible by 5 then you call displayMasteredWords.
class Controller: UIViewController {
private var numCalls = 0
#IBAction func nextButtonPressed(_ sender: Any) {
numCalls += 1
if numCalls % 5 == 0 {
displayMasteredWords()
}
}
private func displayMasteredWords() {
masteredWordsView.isHidden = false
masteredWordsView.text = "You just mastered:\(readString)"
}
}

Swift error when I press button more times than there are pizzas in my array list

So I just started programming and I am now getting this error.
It occurs every time I press the button more times than there are Pizzas in the list.
Full error code: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0):
Here`s my code so far:
import UIKit
var pizzaNumber = 0
var pizzaNames = ["Skinke Pizza", "Salat Pizza", "Pepperoni Pizza"]
let priser = [65,70,65]
var totalProdukt = pizzaNames.count
class ViewController: UIViewController {
#IBOutlet weak var produktNavn: UILabel!
#IBAction func rightButton(_ sender: UIButton) {
pizzaNumber+=1
showPizza()
}
#IBAction func leftButton(_ sender: UIButton) {
pizzaNumber-=1
if pizzaNumber < 0 {
pizzaNumber = 0
}
showPizza()
}
func showPizza() {
if pizzaNumber > totalProdukt {
pizzaNumber = pizzaNames.count
} else {
self.produktNavn.text = pizzaNames[pizzaNumber]
}
}
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.
}
}
You should make sure that pizzaNumber-1 can never be bigger than the number of elements in your array to ensure that you are never trying to access an index that doesn't exist. This can be easily done by changing totalProdukt to be a computed variable. This way, the value of the variable will always be updated when you are trying to access it.
var totalProdukt: Int {
return pizzaNames.count
}
Also bear in mind that array indexing starts at 0, so you need
if pizzaNumber >= totalProdukt {
pizzaNumber = pizzaNames.count-1
} else {
self.produktNavn.text = pizzaNames[pizzaNumber]
}
Bear in mind that with your current code, there's no need for storing the count of the array in a separate variable, since you are only using it at one place in code.
Moreover, the cleanest solution is to check the value before actually increasing it rather than when using it, this way in showPizzas you don't need to do any checks, just update the label:
class ViewController: UIViewController {
#IBOutlet weak var produktNavn: UILabel!
#IBAction func rightButton(_ sender: UIButton) {
if pizzaNumber < pizzas.count-1 {
pizzaNumber+=1
}
showPizza()
}
#IBAction func leftButton(_ sender: UIButton) {
if pizzaNumber > 0 {
pizzaNumber-=1
}
showPizza()
}
func showPizza() {
self.produktNavn.text = pizzaNames[pizzaNumber]
}
}
Here's how to fix your error
func showPizza() {
if pizzaNumber >= totalProdukt { // << Added '=' since pizzaNames[pizzaNames.count] is out of bounds
pizzaNumber = pizzaNames.count - 1
} else {
self.produktNavn.text = pizzaNames[pizzaNumber]
}
}

How to have label text change to message after user types in textfield?

So I am very very new to coding in general and I am taking a class learning about creating apps in xcode. We were assigned to make our first app from scratch. My idea was to make a guessing game where the user is given a picture up close, and they are have to guess what it is. I want it where there's a label, textfield and button. They type their guess in the textfield, click the button to check their answer, and then the label text changes saying "you are wrong" or you are correct". I've looked all over and haven't found much on how to change a label's text depending on what is entered in the textfield. I assumed that I would use an if-else statement but it seems like that is only for integers. Any help on what to do? I've connected everything also. Thanks a lot in advance!
There you go:
import UIKit
import WebKit
class ViewController: UIViewController{
var dictionaryForQuestionAndAnswer = ["what is the best way to learn coding?":"trying","who is best person to ask question?":"Google"]
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var textField: UITextField!
var answerIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
questionLabel.text = ""
}
#IBAction func questionClicked(_ sender: Any) {
let random = randomInt(min: 1, max: 2)
if(random == 1)
{
answerIndex = 1
questionLabel.text = "what is the best way to learn coding?"
}
else
{
answerIndex = 2
questionLabel.text = "who is best person to ask question?"
}
}
#IBAction func answerClicked(_ sender: Any) {
if let answer = textField.text
{
if( answerIndex == 1 && answer == dictionaryForQuestionAndAnswer["what is the best way to learn coding?"])
{
questionLabel.text = "You are correct"
}
else if( answerIndex == 2 && answer == dictionaryForQuestionAndAnswer["who is best person to ask question?"])
{
questionLabel.text = "You are correct"
}
else
{
questionLabel.text = "You are wrong"
}
}
}
func randomInt(min: Int, max:Int) -> Int {
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Output

Switch for rock-paper-scissors

I'm trying to make a simple rock-paper-scissors game. To do it I used a switch statement but for some reason it doesn't work properly.
This is the structure I thought of to make this little game:
There are three buttons for Rock,Scissors,Paper, and you have to choose one.
There is a Label that tells what the opponent (computer) chose, and I named it opponentLabel.
There is a Label that tells what the result is ("you won" for example) and I named it resultLabel
and it works like this (this just the way it's structured):
var a = Int()
if the player chooses Rock ---> a = 0
if the player chooses Paper ---> a = 1
if the player chooses Scissors ---> a = 2
For the opponent (computer, not a person) there is a randomNumber which
could be 0,1,2, and same here, if 0->opponent chose rock, if
1->opponent chose paper, if 2-> opponent chose scissors
and then I wrote a switch statement that puts it all together.
The problem is that for some reason, when I run the app, if I choose rock everything works fine, but when I choose paper or scissors the results are wrong.
For example if I choose Paper (a = 1) and the opponent has paper (which means that the random number happened to be randomNumber = 1), the resultLabel is not "DRAW" as it would be supposed to be, but it is "You Lost": paper and scissors don't work !! What am I doing wrong?
Here is the full code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var opponentLabel: UILabel!
#IBOutlet weak var resultLabel: UILabel!
#IBOutlet weak var rockButton: UIButton!
#IBOutlet weak var paperButton: UIButton!
#IBOutlet weak var scissorsButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Hide()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func Hide() {
opponentLabel.hidden = true
resultLabel.hidden = true
}
func unHide() {
opponentLabel.hidden = false
resultLabel.hidden = false
}
var a = Int()
var randomNumber = Int()
func randomChoice() {
randomNumber = Int(arc4random() % 3)
NSLog("randomNumber%ld", randomNumber)
}
func gameOn() {
switch(randomNumber) {
case 0:
opponentLabel.text = "The opponent chose : ROCK"
if a == 0 {
resultLabel.text = "DRAW"
} else {
if a == 1 {
resultLabel.text = "YOU WON!"
}
if a == 2 {
resultLabel.text = "YOU LOST!"
}
}
unHide()
break
case 1:
opponentLabel.text = "The opponent chose: PAPER"
if a == 0 {
resultLabel.text = "YOU LOST!"
} else {
if a == 1 {
resultLabel.text = "DRAW"
}
if a == 2 {
resultLabel.text = "YOU WON!"
}
}
unHide()
break
case 2:
opponentLabel.text = "The opponent chose: SCISSORS"
if a == 0 {
resultLabel.text = "YOU WON!"
} else {
if a == 1 {
resultLabel.text = "YOU LOST!"
}
if a == 2 {
resultLabel.text = "DRAW"
}
}
unHide()
break
default:
break
}
}
#IBAction func rockButton(sender: AnyObject) {
a == 0
randomChoice()
gameOn()
}
#IBAction func paperButton(sender: AnyObject) {
a == 1
randomChoice()
gameOn()
}
#IBAction func scissorsButton(sender: AnyObject) {
a == 2
randomChoice()
gameOn()
}
}
In your #IBAction functions, you are comparing 'a' with 0, 1 or 2 instead of giving 'a' that value.
Change your code so instead of a == 0, it's a = 0. Do this for the 3 #IBActions and try it again and it should work.

Why won't swift recalculate the 'if' statement?

I'm trying to create a very simple 'guessing game' where the user has to guess how many fingers the computer has up (maximum 5 fingers).
Here's the thing. When the code executes and I press submit, even when the print logs registers a correct number, the app still prints the if statement for incorrect. Where am I going wrong?
import UIKit
class ViewController: UIViewController {
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.
}
#IBOutlet weak var fingerInput: UITextField!
#IBOutlet weak var fingerResult: UILabel!
#IBAction func fingerSubmit(sender: AnyObject) {
let realNumberofFingers = Int(arc4random_uniform(6))
print(realNumberofFingers)
if fingerInput != realNumberofFingers {
fingerResult.text = "Gosh darn, that's wrong!"
} else if fingerInput == realNumberofFingers {
fingerResult.text = "Thats right!"
}
}
}
You are comparing the actual UITextField fingerInput with the realNumberofFingers. That is wrong and will always yield false. What you should do instead is parse the string from the fingerInput and check if the integer contained in that string is equal to realNumberofFingers:
#IBAction func fingerSubmit(sender: AnyObject) {
let input = Int(fingerInput.text!)
if let enteredFingers = input {
let realNumberofFingers = Int(arc4random_uniform(6))
print(realNumberofFingers)
if enteredFingers == realNumberofFingers {
fingerResult.text = "Thats right!"
} else {
fingerResult.text = "Gosh darn, that's wrong!"
}
} else {
fingerResult.text = "Please enter a correct guess - an integer!"
}
}