mnbayan / AutocompleteTextfieldSwift

Simple and straightforward sublass of UITextfield to manage string suggestions
MIT License
290 stars 66 forks source link

fatal error: unexpectedly found nil while unwrapping an Optional value #13

Closed keewong closed 8 years ago

keewong commented 8 years ago

Hi there! What a great add on to the Xcode Swift Community!

I am a beginner programmer, so I really don't know why things do the things they do. Especially in Swift, since I am just starting to learn it.

Anyways, I just wanted to let you know, I don't know if this is a bug or not, but I ran into an issue using your plugin.

I embedded a navigation controller to eventually reach the view that implements your autocomplete plugin, and when i hit "back" it brings up a fatal error, saying that it unexpectedly found nil while unwrapping an optional value.

The line of code where it crashes is line 77 for me: setupAutocompleteTable(newSuperview!) inside the public override func willMoveToSuperview(newSuperview: UIView?)

anyways, not sure if you wanted to try that out and take a look at it, it could also be just me that I may have added some things and didn't realize.

mnbayan commented 8 years ago

@keewong thanks for the feedback. I will look into this.

tejas-ardeshna commented 8 years ago

same issue appears for me.

jkereako commented 8 years ago

The cash is caused by the forced unwrapping of newSuperview in the file AutoCompleteTextField.swift.

public override func willMoveToSuperview(newSuperview: UIView?) {
  super.willMoveToSuperview(newSuperview)
  commonInit()
  setupAutocompleteTable(newSuperview!)
}

Here's Apple's documentation of on it.

A view object that will be the new superview of the receiver. This object may be nil.

The solution is to simply not force unwrap the optional parameter. But the question is, what ought to happen if newSuperview is nil?