I get Thread 1: signal SIGABRT and my IOS App crashes - swift

I am working on an IOS App on Swift and its connected to Firebase. Once I reach a specific page in the run mode, my app crashes and I get this error message without any details. Please help
I do not have any errors with my Outlets and I have tried debugging with break points but it still takes my to the AppDelegate with this error without details. I am clueless. Before it used to work the code should be fine and data were being inputted to the DB but something happened since I have created the login page.
// LoginViewController.swift
// Together
import UIKit
import Firebase
class LoginViewController: UIViewController {
#IBOutlet weak var usernameTxt: UITextField!
#IBOutlet weak var passTxt: UITextField!
#IBAction func userLoginBtn(_ sender: Any) {
var pass = passTxt.text
Auth.auth().signIn(withEmail: usernameTxt.text!, password: passTxt.text!){
(user, error) in
if error == nil{
self.performSegue(withIdentifier: "loginToHome", sender: self)}
else{
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}

I GOT THIS ISSUE SORTED FINALLY and The mistake was very simple. If anyone is facing the same issue and you are sure you don't have any issues with your outlets, go the to Identity inspector and check the Class of each ViewController. Make sure not more than 1 ViewController is connected to the same Class, especially if the class had code for a specific function. This was the reason why my App crashes and takes me to the AppDelegate class. The code of the last page I reach before the crash did not have errors, but the only cause was having it connected to a Class used by another ViewController.

Related

unrecognized selector sent to instance 0x7ff0ece07a40" ERROR on xcode

for actionButton in actionButtons {
if actionButton.currentTitle != (String(rightAnswerPlacement))
{
actionButton.isHidden = true // true to hide the button
}
}
}
#IBOutlet weak var hintButton: UIButton!
//Label for qs
#IBOutlet weak var label: UILabel!
//button for choices
#IBOutlet var actionButtons: [UIButton]!
#IBAction func Action(_ sender: AnyObject)
{
if (sender.tag == Int(rightAnswerPlacement))
{
print("Right")
}
else
{
print("Wrong")
let alert = UIAlertController(title: "That is incorrect", message: "Try again!", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
present(alert, animated: true)
currentQuestion -= 1
//how to record which wrong answer the user is pressing
//let WrongAnswer = sender.tag.titleLabel?.text
}
Hi, this is a snippet of code for a simple quiz app and I am trying to code for a hint button which when pressed makes one of the choices for every qs in the quiz disappear. My code had no errors until I ran it and saw the error mentioned above in the App.Delegate.swift.The exact error message is 'Thread 1: Exception: "-[Aphasia_app.ViewController Hint:]: unrecognized selector sent to instance 0x7ff0ece07a40" ' . I am quite sure the problem is with my code and not with any connections in the storyboard. Thanks for the help, been stuck at this for two days now...
I am quite sure the problem is with my code and not with any connections in the storyboard.
No, the problem is indeed a connection. According to a suggestion in your previous question you renamed Hint to hintButton which does not rename the connection automatically.
In the storyboard delete the dead Hint connection and reconnect it to hintButton.

Attempt to present <UIAlertController> on whose view is not in the window hierarchy

I've been trying to present a UIAlertController when user entered wrong password for their account, the UIAlertController is located in one separate file inside a Model group which I extend the UIViewController class to add this alert functionality to it. I also has another file inside my model group namely LogIn which I wrote all the logic behind the login process so that I can call it to my LogInVC. However, I got an error of "Attempt to present on whose view is not in the window hierarchy!" whenever the function get call inside my LogInVC. I'm trying to make my project in MVC and I know what caused this error but I just don't know how to fix it. May anyone tell me how to fix this problem?
Alert
import Foundation
import UIKit
extension UIViewController {
//MARK: - Not Enough Information Alert
func notEnoughInfo(title: String, message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
//MARK: - Incorrect Username and Password
func wrongInfo(title: String, message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "Try again", style: .default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
LogIn
import Foundation
import Firebase
class LogIn: UIViewController{
let db = Firestore.firestore()
//MARK: - userValidation()
func userValidation(Username:String, Password:String){
db.collection("users").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
if let snapShotDocument = querySnapshot?.documents {
for doc in snapShotDocument {
let data = doc.data()
if let username = data[C.username] as? String, let password = data[C.password] as? String {
if Username == username, Password == password {
print("Log in Successfully")
}
else {
self.wrongInfo(title: "Incorrect password", message: "Try again please")
}
}
}
}
}
}
}
}
LogInVC
import UIKit
import Firebase
class LogInVC: UIViewController {
#IBOutlet weak var emailTextField: UITextField!
#IBOutlet weak var passwordTextField: UITextField!
#IBOutlet weak var logInBtn: UIButton!
let db = Firestore.firestore()
let logIn = LogIn()
override func viewDidLoad() {
super.viewDidLoad()
//logInBtn.layer.cornerRadius = logInBtn.frame.height/5
}
#IBAction func logInBtn(_ sender: UIButton) {
if let username = emailTextField.text, let password = passwordTextField.text{
if username.isEmpty || password.isEmpty{
notEnoughInfo(title: "Not enough information", message: "Please fill in all the necessary information.")
}else{
logIn.userValidation(Username: username, Password: password) //here is where problem occured
//move to another viewcontroller
}
}
}
#IBAction func signUpBtn(_ sender: UIButton) {
let push = storyboard?.instantiateViewController(withIdentifier: C.signUpVC) as! SignUpVC
push.modalPresentationStyle = .fullScreen
present(push, animated: true, completion: nil)
}
} //ends of class
You need to first dismiss the current present alert or present controller. currently you are trying to present controller over a controller that's why it shows this error. Don't present . remove this line from self.wrongInfo(title: "Incorrect password", message: "Try again please") from LogIn.
try this and you can comment again if there is anything regarding this.

How to fix this error ' this class is not key value coding-compliant for the key containerView.'?

When the user what to sign up or login it has error and get this error in the output "this class is not key value coding-compliant for the key containerView."
this is the login code also signup has the same error and l'm sure it's a right codes.
import UIKit
import Firebase
class LoginViewController: UIViewController {
#IBOutlet var Aemeil: UITextField!
#IBOutlet var Apassword: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func login (_ sender : Any){
guard let email = Aemeil.text, let password = Apassword.text
else {
print("Form is not valid")
return
}
Auth.auth().signIn(withEmail: email , password: password , completion: { (user,error) in
if error == nil{
let vc = self.storyboard?.instantiateViewController(withIdentifier: "AHome");
self.present(vc!, animated: true, completion: nil);
print("Home page open")
}
else{
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Yes", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
})
}
}
You have created a binding from a storyboard element, to an element in the corresponding swift file, with the name containerView. After that you have deleted the containerView variable in your swift file, but the binding still exists in the storyboard.
To solve this problem go to your storyboard, click on the view controller, like below:
After that, click on the connection inspector, to see all bindings of your viewcontroller and delete the binding from the containerView:
I have faced the similar problem, I created a segmentview under which I've kept a containerView and removed the default segue that came along with that container view. After that I renamed the container view controller name and connected as show segue. This was the problem which caused me to face this issue. When I changed the show segue to Embed segue the problem resolved.

AlertController trying to be presented on view not in window hierarchy

Trying to figure out why I am getting this error. I have a single view application with two viewcontrollers. I have a segue to the second vc on a button touch:
#IBAction func showAccount(_ sender: Any) {
performSegue(withIdentifier: "toAccountFromRoot", sender: self)
}
I have a facade pattern set up with a Controller file and several helper files. I set the Controller up on the AccountViewController file:
let myController = Controller.sharedInstance
One of the helper files is to set up a central AlertController for simplified alerts (no extra buttons). It has one method, presentAlert that takes a few arguments, alert title, messsage, presenter.
On my AccountViewController I am attempting to present an alert using this code:
myController.alert.presentAlert("No Agent", "You have not registered an agent yet.", self)
and in the AlertManager file, presentAlert looks like this:
func presentAlert(_ title:String, _ message:String, _ presenter:UIViewController) {
let myAlert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
myAlert.addAction(okAction)
presenter.present(myAlert, animated:true, completion:nil)
}
I've done this in quite a few apps without an issue so I am confused as to why the compiler thinks the AccountViewController view is not in the window hierarchy.
Dang it, typed all that and then I solved it 5 seconds later. Just in case someone else runs into the same problem, I was trying to present the alert in viewDidLoad, should of been doing it in viewDidAppear.

Retrieving data by NSUserDefaults Swift 2.0

Currently, my code for NSUserDefaults work fine. The only issue I have now is how can I send the data store to another page? I can get it now save the data, but how do I retrieve that same data via NSUserDefaults to a secondary page?
import UIKit
class MainTableViewController: UITableViewController, UITextFieldDelegate {
let userDefaults = NSUserDefaults.standardUserDefaults()
#IBOutlet weak var name: UITextField!
#IBAction func btnSave() {
if name.text == "" {
let alert = UIAlertController(title: "Data", message: "Missing Name.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
else {
userDefaults.setObject(name.text, forKey:"name")
userDefaults.synchronize()
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
name.text = userDefaults.stringForKey("name")
}
}
You can retrieve your data from your second page with same code.
Create your NSUserDefaults in second page like this ;
First of all add these codes to your FirstPage in IBAction func. Don't use the if else statement for test.
UserDefaults.standard.set("yourValue", forKey: "yourKey")
When you press the button it should be save your data with your key.
In the secondPage you can retrieve your data in your viewDidLoad method like this;
override func viewDidLoad() {
super.viewDidLoad()
print(UserDefaults.standard.value(forKey: "yourKey") ?? "defaultValue in case key is not found")
print(value)
}
It should be print your value to your console (bottom window in your xcode) when the secondPage is open. If you still can't retrieve your value please share to me a your storyboard picture. I want to see your connections.