fatal error: unexpectedly found nil while unwrapping an Optional value - why? - swift

I'm pretty new to coding in Swift and I'm not too sure what's happening here - can anyone help?
Thanks
import UIKit
class SecondViewController: UIViewController {
var toDoItems:[String] = []
#IBOutlet weak var toDoItem: UITextField!
#IBAction func addItem(sender: AnyObject) {
toDoItems.append(toDoItem.text)
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
println(toDoItems)
}
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.
}
}

Most likely, your IBOutlet, toDoItem, was not bound successfully to a UITextField in Interface Builder. Check the outlets for your text field in Interface Builder and make sure it's hooked up successfully.
If the outlet is hooked up properly, another candidate source of this problem is the instantiation of the view controller itself. If you instantiated it programmatically (e.g. SecondViewController() instead of storyboard.instantiateViewControllerWithIdentifier(...)), that would also result in this error.

Related

Cannot get the SceneView to appear on ViewController

I am brand new to Swift so please go easy on me!
I am basically having trouble with rendering an ARKIT ARSCNView with getting the error:
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
Here is my code:
Initializing the view here by connecting to the Storyboard
#IBOutlet weak var sceneView: ARSCNView!
Here is the ViewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
setNeedsStatusBarAppearanceUpdate()
// crashes here
sceneView.delegate = self
}
Heres the ViewDidAppear:
override func viewDidAppear(_ animated: Bool) {
DispatchQueue.main.async {
self.animatePulsatingLayer()
self.dowloadModel()
}
}
The animatePulsatingLayer just plays an animation while the model is downloading.
The Download model just downloads the model to weak var node: SCNNode!
I have seen this code working before but since I have integrated SwiftUI into the project it has stopped working.
Any help would be appreciated.
This error mostly occurs when you remove or do not add the reference of the outlet with the .storyboard or .xib files.
I suggest you check them by right-clicking to the view on the storyboard and check if there is a connection.
Like this:
if there is no such connection you can simply connect them like this:

Error message when setting UICollectionView data source and delegate with custom class

I am creating a sort of manual slideshow (i.e., the pictures move when the user taps a forward or backward button), and it is made with a Collection View. I had to create a custom collection cell view class, which I finished, but now I'm getting an error message in the iOS Simulator, "fatal error: unexpectedly found nil while unwrapping an Optional value" even though my code builds successfully. The error is in the ibCollectionView.dataSource = self line, and because I'm new to Swift and OOP (I learned about a month ago for the first time), I am confused by this error message, especially because there are no ? operators. I have included the problem part of my code (the part Xcode showed with the error message) below. Thank you!
import UIKit
class FluidIntakeMainMenuViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
//MARK: - Collection View Properties
#IBOutlet weak var ibCollectionView: UICollectionView!
#IBOutlet weak var ibCollectionViewLayout: UICollectionViewLayout!
var cellArray:[customCollectionViewCell] = Array(repeatElement(customCollectionViewCell(), count: 6))
let displayedCellDimensions = CGSize(width: 343, height: 248)
//MARK: - Xcode-generated Methods
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
ibCollectionView.dataSource = self
ibCollectionView.delegate = self
}
//My code for this class continues down here...
First and most important thing is to check the IBOutlet connection
that you made in your storyboard.
Try to remove the connection in storyboard and then reconnect it.
Secondly put a debugger on to check if it's still nil.
ibCollectionView.dataSource = self
I think you forgot about this:
override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
ibCollectionView?.register(YourCustomCell.self, forCellWithReuseIdentifier: "Cell")
And use "Cell" identifier in cellForItemAt...

Swift Promises - Reload Data method is not binding the data

I'm using promises to retrieve information some methods via JSON. I'm filling the information with the function:
I'm trying to set those records in my TableView with:
#IBOutlet weak var usersTableView: UITableView!
var dataSource: [UserResponse]? {
didSet {
self.usersTableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
loadFriends()
// Do any additional setup after loading the view.
}
func loadFriends() {
//UserService.getFriends()
let (request, promise) = UserService.getFriends()
promise.then { user in
self.dataSource = user
}.catch{
error in
SCLAlertView().showError("Error", subTitle: error.localizedDescription)
}
}
But is returning the error:
fatal error: unexpectedly found nil while unwrapping an Optional value
How I can fix this error?
Your usersTableView is an implicitly unwrapped optional (UITableView!). When you call the dataSource setter, the didSet property observer tries to call reloadData() on usersTableView, but it is nil, so you get a crash - you cannot call methods on nil.
It looks like you haven't connected the outlet to your usersTableView property in your storyboard. Alternatively, you're setting it to nil somewhere else in your code. This could be happening automatically if usersTableView isn't part of your view hierarchy when the view controller is loaded, since it is a weak variable, but I expect you have added it as a subview in the storyboard?

Swift: I get Debugging "errors" when I try to make my program work

I followed an online tutorial on youtube for a Hello World like program for Swift. Even though I typed the code just like the code in the tutorial it till gives me a debugging error (the green errors). I researched problems like this but the code was to complicated and was over my head. Usually I code in java, but I have been wanting to develop for mobile. Anyways, if you can tell me:
1. What the error means.
2. How I can fix it.
3. How can I prevent errors like this.
NameLabel.text = "Hi (nameTextField.text)" is the line I get the error on.
Here is my code:
import UIKit
class ViewController: UIViewController {
#IBOutlet var NameLabel: 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 HelloWorldAction(nameTextField: UITextField) {
NameLabel.text = "Hi \(nameTextField.text)"
}
}
nameTextField is't #IBAction. It's #IBOutlet.
I made a button and it appropriated action. I have everything working.
#IBAction func button(sender: UIButton!) {
NameLabel.text = "Hi \(namedTextField.text)"
}

fatal error with message post using parse

So I am new to using Parse and having some issues with posting a message through the back-end. I am able to set up a user account but when I try to submit the post I am getting an error that says..."fatal error: unexpectedly found nil while unwrapping an Optional value" ...?
Here is my code:
class ComposeViewController: UIViewController {
#IBOutlet var travelTextView: UITextView!
#IBOutlet var charRemainingLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func sendPost(sender: AnyObject) {
var travelPost:PFObject = PFObject(className: "travelposts")
travelPost["content"] = travelTextView.text
travelPost["poster"] = PFUser.currentUser()
travelPost.saveInBackgroundWithTarget(nil, selector: nil)
self.navigationController?.popToRootViewControllerAnimated(true)
}
you cannot pass nil for selector.
travelPost.saveInBackgroundWithTarget(nil, selector: nil);
do this to just save the object.
travelPost.saveInBackground();
But you want to get notified when the object is saved use