I am trying to code a simple app where you input 2 numbers in textfields and return the sum of them in iOS app. The problem is that I am getting Thread 1 signal SIGABRT error and I can't seem to find any fix for this.
This is the code:
#IBOutlet var result: UILabel!
#IBOutlet var firstnum: UITextField!
#IBOutlet var secondnum: UITextField!
#IBAction func add (sender: UIButton) {
var a = firstnum.text.toInt();
var b = secondnum.text.toInt();
var c = a!+b!;
result.text = "\(c)";
}
You should check your connections; if you have a label, textfield, etc connected from the main storyboard to the view controller, check to see if your label in Main.storyboard is linked to it by opening the right side panel and selecting the last bar. It will show you all the outlets that label has.
This occurs when you make a connection from the Main.storyboard to the ViewController.swift, and you remove the text in the ViewController.swift, but you do not remove the connection.
For more information, watch this video:
iOS Programming: How to fix thread 1 signal sigabrt in xcode 6
I hope this helps.
Related
I started coding about 2 weeks ago and chose Swift for iOS development purposes and I am having quite a struggle with debugging. This project is a simple Tic Tac Toe app and everything runs fine except for when I tap on one of my UILabels for an X to appear.
I keep getting this Thread 1: EXC_BAD_ACCESS (code=2, address=0x1134ca968) error on my canTap and I don't know how to fix it.
#IBAction func onTappedGridLabel(_ sender: UITapGestureRecognizer) {
if gameOver {
return
}
var canPlay = false
for label in labels {
let selectedPoint = sender.location(in: background)
if label.frame.contains(selectedPoint) {
if label.canTap {
if(xTurn) {
label.text = "X"
}
else {
label.text = "O"
}
xTurn = !xTurn
label.canTap = false
checkForWinner()
}
}
if label.canTap {
canPlay = true
https://www.dropbox.com/s/amokuept1crt8i0/Tic%20Tac%20Toe%202.0.zip?dl=0
Here's a link with the entire project if anyone is interested. I think it's pretty short.
The problem is that a UILabel has no canTap property. Your label subclass GridLabel has that property, but you have not designated that your labels in the storyboard are instances of your label subclass, so they are just ordinary labels.
In other words, you have this code:
#IBOutlet weak var gridLabel0: GridLabel!
#IBOutlet weak var gridLabel1: GridLabel!
#IBOutlet weak var gridLabel2: GridLabel!
#IBOutlet weak var gridLabel3: GridLabel!
#IBOutlet weak var gridLabel4: GridLabel!
#IBOutlet weak var gridLabel5: GridLabel!
#IBOutlet weak var gridLabel6: GridLabel!
#IBOutlet weak var gridLabel7: GridLabel!
#IBOutlet weak var gridLabel8: GridLabel!
var labels = [GridLabel]()
But you are lying in every one of those lines. None of your labels is a GridLabel. They are all ordinary UILabels. Hence, as soon as you try to access the canTap of one of them, you crash.
In the storyboard, select the nine grid labels and, in the Identity inspector, change their class to GridLabel. Problem solved.
This question already has answers here:
Fatal error: unexpectedly found nil while unwrapping an Optional values [duplicate]
(13 answers)
Closed 2 years ago.
I've been wanting to code for a while, and just started using my free time to try to learn so I know very little but am eager to learn! Also first time poster.
I'm making a simple dice game app, and I've been horribly stuck trying to unwrap strings and variables. I keep getting the error "Unexpectedly found nil while implicitly unwrapping an Optional value" when trying to assign a user input name to a label on the next view controller.
Let me know if this is too convoluted off an app design, but this is what I'm trying to design:
first view controller where 2-4 players enter their names. They hit the next button and it takes them to the next viewController where there are dice, a roll button, and the names they just entered.
I have the UITextField and UILabel for the names correctly connected to IBOutlets.
I forced the UITextField to unwrap by making them optional to prevent the app from crashing, but the names are still not updated.
Here's my code:
class ViewController: UIViewController, UITextViewDelegate, UITextFieldDelegate {
#IBOutlet weak var name1: UITextField!
#IBOutlet weak var name2: UITextField!
#IBOutlet weak var name3: UITextField!
#IBOutlet weak var name4: UITextField!
//connects name strings
#IBOutlet weak var dice1: UIImageView!
#IBOutlet weak var dice2: UIImageView!
#IBOutlet weak var dice3: UIImageView!
#IBOutlet weak var dice4: UIImageView!
//links dice images
#IBOutlet weak var name1Label: UILabel!
#IBOutlet weak var name3Label: UILabel!
#IBOutlet weak var name4Label: UILabel!
#IBOutlet weak var name2Label: UILabel!
//links name labels on second viewController
var diceArray = ["dice1", "dice2", "dice3", "dice4", "dice5", "dice6"]
//creates an array to set dice images
var randomArray = [0,0,0]
var randomDice1 : Int = 0
var randomDice2 : Int = 0
var randomDice3 : Int = 0
//creates variables to hold random number
func storeNames () {
name1Label?.text = name1.text. THIS IS WHERE I KEEP GETTING FATAL ERRORS
name2Label?.text = name2.text
name3Label?.text = name3.text
name4Label?.text = name4.text
//stores the user inputed names into an array
}
//stores the names from textfield into name strings
override func viewDidLoad() {
super.viewDidLoad()
Thread.sleep(forTimeInterval:0.25)
//extends loading screen
name1?.placeholder = "Player 1 Name"
name2?.placeholder = "Player 2 Name"
name3?.placeholder = "Player 3 Name"
name4?.placeholder = "Player 4 Name"
//Creates placeholder text
let tap = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing))
view.addGestureRecognizer(tap)
//removes keyboard when you tap outside keyboard
}
#IBAction func nextButton(_ sender: UIButton) {
storeNames()
}
You want to
assign a user input name to a label on the next view controller.
But all your labels, and text fields seem to be declared as outlets on the same view controller.
You cannot directly use next / previous view controller's outlets like this.
What you need to do is collect all naems in the first view controller, pass them to the next view controller and set them as label text in the next view controller. If you are using storyboards and segues, you can leverage prepareForSegue:sender:, method to do this.
Since you mention you have just started, and are learning things Here are some online tutorials on passing data between views:
https://www.hackingwithswift.com/example-code/system/how-to-pass-data-between-two-view-controllers
https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/
https://matteomanferdini.com/how-ios-view-controllers-communicate-with-each-other/
P.S. This is a very basic approach, but not a recommended one. View transitions and data should not be mixed together in the same code. If you are planning to use this for building a robust app it will be a good idea to also start learning SOLID principles, and design patterns.
There is three main points to understand the question:
1- There's a custom made class "Monster" that inherits from UIImageView superclass
2-There're two images(game characters) in the UI that user has to choose between them
3-According to this choice, one of the two images will appear and the other will be staying hidden
-Each of the images is connected to the UI with its own image
#IBOutlet weak var rockMonster: UIImageView!
#IBOutlet weak var fireMonster: UIImageView!
var activeCharacter: Monster!
-According to which button is pressed the Monster object will carry the result which differs in the image and the type of character
#IBAction func fireMonsterChoosed(sender: AnyObject) {
activeCharacter = Monster (typ: "fireMonster", frame: fireMonster.frame)
activeCharacter.hidden = false
#IBAction func rockMonsterchoosed(sender: AnyObject) {
activeCharacter = Monster (typ: "rockMonster", frame: rockMonster.frame)
activeCharacter.hidden = false
-Now, the activeCharacter var even after equalizing its .frame property with the one of the chosen character, it still has no control on the image so it's not connected with the UI
activeCharacter.hidden = false //No images appear
Q : I need to give activeCharacter var the control over these images in UI after the initialization I make in each IBAction.
Thanks in advance.
I have a +1 button and a -1 button and a label that starts at 0. My +1 button is working great, but for some reason, my -1 button isn't working. Can anyone help?
var sales = 0
#IBOutlet weak var numberOfSalesLabel: UILabel!
#IBOutlet weak var minusOneSaleOutlet: UIButton!
#IBAction func plusOneSale(sender: AnyObject) {
sales += 1
numberOfSalesLabel.text = "\(sales)"
if sales >= 1 {
minusOneSaleOutlet.hidden = false
}
}
override func viewDidLoad() {
}
#IBAction func minusOneSale(sender: AnyObject) {
sales -= 1
numberOfSalesLabel.text = "\(sales)"
if sales == 0 {
minusOneSaleOutlet.hidden = true
}
}
Anybody got any ideas why my minus button isn't working? I'm thinking it might have to do with me calling it as an outlet and as an action, but I'm not sure. Thanks!
p.s.-I'm not sure if this is normal.
Using the connections inspector you'll need to make sure all the connections are correct.
If at any point you delete an #IBAction from your code, then create another connection, the old one will still remain until you remove it properly from the connection inspector.
Each of your buttons should only be connected to a single #IBAction (touchUpInside)
Below is what the connections inspector looks like. The image is from one of my own projects, and shows a connected delegate. So yours will look a bit different.
If all else fails, remove all connections on this view (click the x) and link them up again by Ctrl dragging from your button, into the code viewer right on top of your #IBAction. Sometimes its just easier to start again.
class ViewController: UIViewController
{
#IBOutlet weak var simpleTextField: UITextField!
#IBOutlet weak var simpleLabel: UILabel!
#IBAction func changeLabel(sender: AnyObject)
{
simpleLabel.text = "salam, " + simpleTextField.text + "!";
}
}
I am a new swift learner and I'm trying to launch a simple app. I drag a label to storyboard. At first when I run the app it shows signal SIGABRT error. I cant solve the problem and I rewrite the code then when I try to run the app I have a problem. and when I run the app it show error breakpoint 1.5 its about
how can fix it?
thank you!
I have experienced that before.
Check #IBOutlet and #IBAction in storyboard.
Watch out for following point
・Not double connection.
・Not forget to delete connection that is not used.
That's all.
Check if all IBOutlet and IBAction are connected to their respective code.And also change the your IBAction code to this following
#IBAction func changeLabel(sender: AnyObject) {
simpleLabel.text = "salam \(simpleTextField.text) !"
}