Ошибка «Thread 1: signal SIGABRT» при переходе на другой viewController по нажатию кнопки — swift
Ошибка «Thread 1: signal SIGABRT» при переходе ко второму viewController (класс «Type1ViewController»), перепробовал все не могу пофиксить, вот код из ViewController.swift и скрин Main.storyboard: Классы view-шек проверил совпадают. Ошибка также возникает при отсутствии всех TextField-ов на втором экране. Ошибка отображается вот здесь в AppDelegate:
import UIKit var position: Int = 1 var positionSecondBack: Int = 0 var answer: String = "" //======================================================================= //------------------------ first view controller ------------------------ //======================================================================= class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate < @IBOutlet weak var LabelTypeEc: UILabel! @IBOutlet weak var LabelType: UILabel! @IBOutlet weak var Button1: UIButton! @IBOutlet weak var Button2: UIButton! @IBOutlet weak var Button3: UIButton! @IBOutlet weak var Next1Butt: UIButton! @IBOutlet weak var ProblemTypePicker: UIPickerView! @IBOutlet weak var PickerAP: UIPickerView! @IBOutlet weak var PickerDP: UIPickerView! @IBOutlet weak var PickerV: UIPickerView! override func viewDidLoad() < super.viewDidLoad() ProblemTypePicker.layer.cornerRadius = 15 PickerAP.layer.cornerRadius = 15 PickerDP.layer.cornerRadius = 15 PickerV.layer.cornerRadius = 15 LabelTypeEc.layer.cornerRadius = 5 LabelType.layer.cornerRadius = 5 Button1.layer.cornerRadius = 15 Button2.layer.cornerRadius = 15 Button3.layer.cornerRadius = 15 Next1Butt.layer.cornerRadius = 15 Button1.setTitle(" Вопросы ", for: .normal) Button2.setTitle(" Вопросы ", for: .normal) Button3.setTitle(" Вопросы ", for: .normal) ProblemTypePicker.delegate = self ProblemTypePicker.dataSource = self PickerAP.delegate = self PickerAP.dataSource = self PickerDP.delegate = self PickerDP.dataSource = self PickerV.delegate = self PickerV.dataSource = self >var list1 = ["аннуитентные платежи", "дифференцированые платежи", "вклады"] var list2 = ["1-ый тип", "2-ый тип", "3-ый тип"] var list3 = ["4-ый тип", "5-ый тип", "6-ый тип"] var list4 = ["7-ый тип", "8-ый тип", "-"] //how much stacks in pickerView func numberOfComponents(in pickerView: UIPickerView) -> Int < return 1 >func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int < var countrows : Int = list1.count if pickerView == ProblemTypePicker < countrows = self.list2.count >return countrows > func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? < if pickerView == ProblemTypePicker < let titleRow = list1[row] return titleRow >else if pickerView == PickerAP < let titleRow = list2[row] return titleRow >else if pickerView == PickerDP < let titleRow = list3[row] return titleRow >else if pickerView == PickerV < let titleRow = list4[row] return titleRow >return "" > func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) < if pickerView == ProblemTypePicker < //buttons witch allow us go to "Type docs" //and //PickerViews what contain from "1 Type" to "9 Type" if row == 0 < position = 1 PickerAP.isHidden = false PickerDP.isHidden = true PickerV.isHidden = true Button1.isHidden = false Button2.isHidden = true Button3.isHidden = true >else if row == 1 < position = 4 PickerAP.isHidden = true PickerDP.isHidden = false PickerV.isHidden = true Button2.isHidden = false Button1.isHidden = true Button3.isHidden = true >else if row == 2 < position = 7 PickerAP.isHidden = true PickerDP.isHidden = true PickerV.isHidden = false Button3.isHidden = false Button2.isHidden = true Button1.isHidden = true >> //what type of problem if pickerView == PickerAP < if row == 0 < position = 1 >else if row == 1 < position = 2 >else if row == 2 < position = 3 >> if pickerView == PickerDP < if row == 0 < position = 4 >else if row == 1 < position = 5 >else if row == 2 < position = 6 >> if pickerView == PickerV < if row == 0 < position = 7 >else if row == 1 || row == 2 < position = 8 >> if position == 1 < Next1Butt.isHidden = false >else if position == 2 < Next1Butt.isHidden = true >else if position == 3 < Next1Butt.isHidden = true >else if position == 4 < Next1Butt.isHidden = true >else if position == 5 < Next1Butt.isHidden = true >else if position == 6 < Next1Butt.isHidden = true >else if position == 7 < Next1Butt.isHidden = true >else if position == 8 < Next1Butt.isHidden = true >else if position == 9 < >> > //======================================================================= //---------------------- 1 Type1ViewController 1 ------------------------ //======================================================================= class Type1ViewController: UIViewController < @IBOutlet weak var LabelType: UILabel! @IBOutlet weak var ButtonBack: UIButton! @IBOutlet weak var ButtonNext: UIButton! @IBOutlet weak var Label1: UILabel! @IBOutlet weak var Label2: UILabel! @IBOutlet weak var Label3: UILabel! @IBOutlet weak var Lanel4: UILabel! //textFields @IBOutlet weak var TextField_1Type_1: UITextField! @IBOutlet weak var TextField_2Type_1: UITextField! @IBOutlet weak var TextField_3Type_1: UITextField! @IBOutlet weak var TextField_4Type_1: UITextField! override func viewDidLoad() < super.viewDidLoad() ButtonBack.layer.cornerRadius = 15 ButtonNext.layer.cornerRadius = 15 >>
Как исправить ошибку в XCode (Thread 1: SIGABRT)?
«Thread 1: signal SIGABRT» и приложение даже не открывается (что в общем то не странно).
Приложение для расчета индекса массы тела. Окна задал, связь с ними задал.
// // FirstViewController.swift // Mass App // // Created by *** on 30.08.16. // Copyright © 2016 ***. All rights reserved. // import UIKit class FirstViewController: UIViewController < @IBOutlet weak var ageTextField: UITextField! @IBOutlet weak var heightTextField: UITextField! @IBOutlet weak var weightTextField: UITextField! @IBOutlet weak var sexSegmentedControl: UISegmentedControl! @IBOutlet weak var activitySegmentedControl: UISegmentedControl! @IBOutlet weak var resultsLabel: UILabel! @IBAction func calculateTapped(sender: AnyObject) < weak var activitySegmentedControl: UISegmentedControl! func calculateTapped(sender: AnyObject) < var bmr: Double = 0 var bmi: Double = 0 if let age = Int(ageTextField.text!) < if let height = Int(heightTextField.text!) < if let weight = Int(weightTextField.text!) < switch sexSegmentedControl.selectedSegmentIndex < case 0: bmr = 88.362 + 13.397 * Double(weight) + 4.799 * Double(height) - 5.677 * Double(age) case 1: bmr = 447.593 + 9.247 * Double(weight) + 3.098 * Double(height) - 4.330 * Double(age) default: bmr = 0 >bmi = Double(weight) / pow(Double(height) / 100, 2) > > > let factor = [1.375, 1.55, 1.725, 1.9] let selectedFactor = factor[activitySegmentedControl.selectedSegmentIndex] bmr *= selectedFactor resultsLabel.text? = "Вы должны потреблять \(Int(bmr)) килокалорий для поддержания веса.\nИндекс массы тела \(Int(bmi))." UIApplication.sharedApplication().keyWindow!.endEditing(true) > > 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. >>
2016-08-30 19:05:45.409 Mass App[4618:118258] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key activityTextField.'
Предполагаю что ошибка в том что я до этого вместо activitySegmentedControl писал activityTextField.
Где то это осталось и не дает запуску? Или что.
- Вопрос задан более трёх лет назад
- 4936 просмотров
1 комментарий
Оценить 1 комментарий
How To Solve SIGABRT Error in Xcode
Describe your app idea
and AI will build your App
What Does “Thread 1: Signal SIGABRT” Mean?
The error SIGABRT stands for “signal abort”. It’s a signal that’s sent by iOS – the operating system – to a running app, which will immediately quit the app because of a runtime error. It essentially means your app has crashed…
To break it down further, think of your app as a series of tasks or ‘threads’ running simultaneously. Each thread is responsible for a specific job. When one of these threads encounters a problem it can’t handle, it sends out a distress signal, in this case, the ‘SIGABRT’.
The ‘Thread 1’ typically refers to the main thread where most of your app’s core functions run. When this main thread faces an issue, it’s a significant concern because it can halt the entire operation of your app. This is why understanding and resolving the SIGABRT error is crucial for a smooth user experience.
The problem with the SIGABRT error is that it’s too generic. Xcode is basically saying: “Look, your app has crashed, that’s all we know.” In most cases of the SIGABRT error, you get little information about what’s caused the error.
Before we go on, let’s discuss a few misconceptions and common pitfalls of SIGABRT:
- The SIGABRT error usually has nothing to do with the AppDelegate class declaration, even though it highlights that line in Xcode. The line is highlighted because it’s the first line of code of your app. Don’t waste your time looking in the AppDelegate class, unless you’re absolutely certain the bug is in there.
- The stacktrace is a list of function calls that lead up to the app crashing. That doesn’t mean the line of code that caused the error is anywhere in the stacktrace. It sometimes is, but in other cases, the stacktrace merely leads to the code that choked on a value you set elsewhere in your own code.
- Don’t stare yourself blind on a SIGABRT error. There’s a rational, logical cause for the error. It’s probably a bug in your own code, and there’s nothing wrong with that. Apps aren’t magic, no one is out to get you, and bugs never appear out of the blue. Don’t frustrate yourself with thoughts like “It ran fine yesterday!” – it always does, and now it doesn’t!
Now that we’ve established a baseline, let’s get to the first cause of SIGABRT.
Check Your Outlets
A common cause of “Signal SIGABRT” is a typo or bug in your outlets. Here’s what happened:
- You created a new view controller in Interface Builder, and set it up with a few UI elements like buttons and labels
- You connected these UI elements to your code by using outlet properties, which creates a connection between a property of your view controller and the UI element in Interface Builder
- At one point you changed the name of the initial outlet property and your app started crashing with a SIGABRT error
Outlets in Xcode serve as bridges between the visual components you design in Interface Builder and the code that powers their functionality. Think of them as electric outlets in your home; if the connection isn’t secure or if there’s a mismatch, things won’t work as expected.
Similarly, in app development, even a small oversight like renaming an outlet without updating its connection can lead to errors. This is because the system expects a clear path between the UI element and its corresponding code. When this path is broken or misdirected, the system raises an alarm in the form of the SIGABRT error.
When you’re using Interface Builder to create a view controller, your app will use the XIB file to generate the view controller’s UI when your app runs (roughly speaking). At this point it will also connect outlets from the XIB to properties of the view controller class.
If you’ve changed the name of an outlet property, your app can’t find it anymore. And because of that it will throw an exception. What’s causing the SIGABRT error, is not handling that exception.
Here’s what that looks like in Xcode:
See what’s happening? The property is called otherButton, but the outlet is still called button. At one point we changed the outlet – because the new name is better – and confused the app, which made it crash.
At the top of the stacktrace we also spot another clue:
Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key button.
What does that mean? The app is telling us at this point that the view controller is not key value coding compliant for the key button. This means that it cannot find the button property on the view controller. And that’s true, because we’ve renamed it.
iOS uses a mechanism called key value coding to inspect the properties a view controller has, so it can use those properties to reference UI elements it has created based on the XIB.
How do you solve the bug at this point? You can use 2 approaches:
-
You rename the property back to its original name
Quick Tip: Just as a changed @IBOutlet can cause “Thread 1: signal SIGABRT”, so can erroneously changing the name of an action, i.e. with @IBAction, cause the SIGABRT error.
Check The Stacktrace
In many cases Xcode won’t show you any helpful error messages for a SIGABRT crash. When that happens, it’s useful to know a few debugging commands, such as bt.
The stacktrace is akin to a detective’s notebook in the world of app development. When something goes wrong, it provides a chronological record of events leading up to the error. By examining this record, developers can trace back to the origin of the problem.
Think of it as retracing your steps when you’ve lost something. Each entry in the stacktrace represents a function or method call, and by examining these, you can pinpoint where things started to go awry. The ‘bt’ command is a handy tool in this investigation, offering a concise view of this record.
Xcode has an integrated debugging environment called LLDB. It’s what you see at the bottom of Xcode when your app runs, the Console or debug output area. You often see debug messages here, but did you know you can also use it to input commands?
Next time your app crashes, try typing help in LLDB. Like this:
You’ll see that many of the LLDB commands directly correspond to actions you can take with the debugger, such as setting breakpoints, stepping over lines of code, and inspecting runtime values.
One command is particularly useful. You can type in bt to see the current call stack (also called “backtrace” or “stacktrace”). This is a list of all functions that ran up to the current crash. This trace typically includes the function that caused a bug.
Here, check out the stacktrace of a typical Index out of range error. In the screenshot below, we’ve deliberately caused that error by getting index 99 from an array that only has 4 items. When the app crashes, bt can tell us which line of code caused the error.
Can you spot the following information in the stacktrace?
- The offending code is at line 21 of ViewController.swift, inside the viewDidLoad() function
- You can even see that we used the subscript “getter” of Array
- Before the crash a whole bunch of view controller-related function calls were made
Based on the information we got with bt, we can find the offending line in our code and fix it. Xcode already helped us in this case, by highlighting the error in the editor. In some scenarios you won’t have such luck, and then it can be helpful to use the bt command.
One last thing: you can inspect values at runtime with the print command. In the above scenario, typing print names would have produced this output:
([String]) $R0 = 4 values [0] = “Ford”
[1] = “Arthur”
[2] = “Zaphod”
[3] = “Trillian”
>
For printing complex objects, use po. Awesome!
Keep in mind that a stacktrace runs outside-in. The bottom of the stack trace shows top-level function calls, and the higher up the stack you go, the deeper the calls go in. The latest, most recent, deepest-level call is at the top of the stack.
Make an Exception Breakpoint
An exception breakpoint is triggered whenever an exception occurs in your code. Instead of specifying on which line the breakpoint is triggered, you instruct the debugger to halt code execution for exceptions.
Imagine you’re reading a book, and every time there’s a plot twist, you place a bookmark. These bookmarks help you quickly revisit crucial turning points in the story. In the realm of coding, exception breakpoints act like these bookmarks.
They automatically mark the exact point where something unexpected happens, allowing developers to pause and inspect the situation. By setting up these ‘bookmarks’, you’re essentially telling Xcode, ‘Hey, if something unusual happens, stop right there and let me take a closer look.’ This proactive approach can save a lot of time and frustration during the debugging process.
Exception breakpoints are useful for inspecting the code when an exception occurs. You can see which line of code threw the exception, and you can inspect values in your code at that point. Some exceptions are caused by bugs or invalid states of your app, so exception breakpoints are useful for finding and fixing those bugs.
Here’s how you can set an exception breakpoint:
-
Go to the Breakpoint navigator in Xcode, by using the tabs on the left
When an exception is thrown, execution of your app halts. You can now use the debugger to inspect values, step through the code, and use LLDB commands. When possible, Xcode will take you to the line of code that caused the exception.
Keep in mind that an exception doesn’t necessarily crash your app! So, whenever the exception breakpoint is enabled, and an exception occurs, your app is halted. Halting code with a breakpoint isn’t the same as an app crash, so don’t let that confuse you.
For example, an exception breakpoint will get triggered by an Unsatisfied constraints exception, but that won’t crash your app. Use the exception breakpoint to gather extra information for the SIGABRT crash, and then disable it once you’ve solved the bug (until it’s needed again).
Conclusion
The SIGABRT error is quite cryptic, and can prove difficult to solve. Why can’t Xcode just give helpful error messages? Well, that’s a good question…
The short answer is that there are so many moving parts in iOS development that Xcode can’t always determine the cause of a crash. Xcode doesn’t know that you erroneously changed the name of an outlet. It only knows that in connecting the outlet, some code was invoked, and that caused an exception.
This means you’ll always see an error that’s as close to the root cause as possible. The best you can do is make lots of mistakes, decrypt lots of error messages, and get to know them better. And what you’ve learned in this tutorial, is how to find and solve the SIGABRT error!
Swift error : signal SIGABRT how to solve it
I’m just a beginner in Swift coding. My idea is quite simple which is an app with two buttons. When clicked, a textfield will change its text. In the Main.StoryBoard, I add a textfield and two buttons. In ViewController.swift file. I write as this:
import UIKit class ViewController: UIViewController < @IBOutlet weak var textfield: UITextField! @IBOutlet weak var button: UIButton! @IBOutlet weak var button2: UIButton! @IBAction func action1(_ sender: UIButton) < textfield.text="you just clicked on button1" >@IBAction func action2(_ sender: UIButton) < textfield.text="you just clicked on button2" >>
It is supposed to be all right. However, an error appears which shows:
thread1:signal SIGABRT
in file AppDelegate.swift line:
class AppDelegate: UIResponder, UIApplicationDelegate
What is wrong with my code?
6,707 7 7 gold badges 42 42 silver badges 63 63 bronze badges
asked Apr 21, 2017 at 15:23
814 2 2 gold badges 10 10 silver badges 26 26 bronze badges
You didn’t properly connect your outlets.
Apr 21, 2017 at 15:29
I tried but failed to solve it. what do u mean by properly? for the code. how it is possible to identify two different buttons with the same code?
Apr 21, 2017 at 15:41
did you connected the outlet to a button and then changed the outlet name by any chance?
Apr 21, 2017 at 18:38
a UIView object may still have an old connection too. Maybe try going through each of them by Ctrl+Click to bring up their connections or look at the Connections panel to the right. Double check theyre only connected where necessary
Apr 22, 2017 at 1:51
15 Answers 15
You get a SIGABRT error whenever you have a disconnected outlet. Click on your view controller in the storyboard and go to connections in the side panel (the arrow symbol). See if you have an extra outlet there, a duplicate, or an extra one that’s not connected. If it’s not that then maybe you haven’t connected your outlets to your code correctly.
Just remember that SIGABRT happens when you are trying to call an outlet (button, view, textfield, etc) that isn’t there.
answered Apr 21, 2017 at 23:06
Andy Lebowitz Andy Lebowitz
1,471 2 2 gold badges 16 16 silver badges 24 24 bronze badges
thanks for your help. I fixed the problem by recreate a new button item which means the outlet was wrong before. it there any way to view all outlet connections?
Apr 22, 2017 at 6:55
Yep! Go to your storyboard, find the view controller of which you want to view all the outlets of, and click on the yellow logo at the top of it which shows a square inside a circle. Then go to the side panel on the right, and click the arrow with the circle (top right). There you can see all the outlet and action connections.
Apr 22, 2017 at 19:39
Got this when I made an IBOutlet when I meant to make an IBAction, and thought I removed the outlet (I didn’t)
Apr 2, 2018 at 20:50
@RobertDundon make sure u remove the outlet in the code, AND in the storyboard connections tab, or you will get an error
Apr 13, 2018 at 15:12
I have this problem with tests, and it seems nothing to do with storyboards/appdelegates
Jan 15, 2019 at 9:29
For me it wasn’t an outlet. I solved the problem by going to the error And reading what it said. (Also Noob..)
This was the error:
And The solution was here:
Just scroll up in the output and the error will be revealed.
answered Jan 10, 2018 at 18:33
6,398 4 4 gold badges 27 27 silver badges 37 37 bronze badges
Note, also check the spelling in the code. When you call dequeueReusableCell(withIdentifier: «cellIdentifier», for: indexPath) , if the withIdentifier parameter is not the same as the Identifier property; then, this error will be thrown.
Apr 5, 2018 at 13:21
To solve the problem, first clean the project and then rebuild.
To clean the project, go to MenuBar: Product -> Clean
Then to rebuild the project, just click the Run button as usual.
2,475 18 18 silver badges 28 28 bronze badges
answered Jan 3, 2018 at 6:47
Marwan Salim Marwan Salim
682 7 7 silver badges 14 14 bronze badges
A common reason for this type of error is that you might have changed the name of your IBOutlet or IBAction you can simply check this by going to source code.
Click on the main.storyboard and then select open as and then select source code
source code will open
and then check whether there is the name of the iboutlet or ibaction that you have changed , if there is then select the part and delete it and then again create iboutlet or ibaction. This should resolve your problem