1

Error from xcode

So I'm trying to create a user on my database backend, everything is working on the app bar this segment on xcode, written in swift.

The backend can accept new users via this method but for some reason swift won't have it.

When I click register I get the error shown in the photo, code used

Alamofire.request(.POST, urlRequest, parameters: ["X-API-KEY": API_GENERAL_KEY,"username":userName.text!,"email": userMail.text!,"password":userPassword.text!,"profile_photo": self.imageName])

and the output is

<spotimusic.RadioStyleCollectionViewController: 0x7fe313d8fd00> whose view is not in the window hierarchy!
SDSD -  Optional("nathan@trojan-audio.com")
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) 

Another error which shows in a segment is

Error 2 from xcode

Thanks for any help or suggestions

Julien Quere
  • 2,407
  • 16
  • 21
  • Try rewriting the line without the force unwrapping. Test that your values are non-nil instead. (e.g. `if let...`) – Phillip Mills Aug 28 '16 at 13:29
  • *unexpected found nil* is the #1 error in Swift. Please read the chapter about optionals in the Language Guide and [this topic](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – vadian Aug 28 '16 at 13:38
  • Thanks Vadian currently reading this – Nathan Trojanaudio Bridge Aug 28 '16 at 13:43
  • Likely problems include either (a) failure to hook up the outlet correctly; (b) instantiating the view controller incorrectly; or (c) `imageName` is an optional that was never set. I'd suggest you expand the `self` section in the variables panel in the lower left of your first screen snapshot and see which of these is `nil`. – Rob Aug 28 '16 at 13:49

2 Answers2

0

It seems that userName.text or userPassword.text doesn't have a value. This error can occur due to an unlinked outlet.

If your outlets are linked properly try this:

guard let userName = userName.text, let userMail = userMail.text, let userPassword = userPassword.text else {
    return
} 
Alamofire.request(.POST, urlRequest, parameters: ["X-API-KEY": API_GENERAL_KEY,"username":userName,"email": userMai,"password":userPassword,"profile_photo": self.imageName])

You will be able to catch the empty values without crashes.

Let me know if it fixes your problem

Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49
  • This fixed one of the crashes but I'm getting the THREAD 1: BAD_EXC_INSTRUCTION error in this field if userName.text == "" || userMail.text == "" || userPassword.text == "" || self.imageName == "" – Nathan Trojanaudio Bridge Aug 28 '16 at 13:49
  • @NathanTrojanaudioBridge if you are using my guard statement, then check userName.isEmpty, userMail.isEmpty and userPassword.isEmpty – Marco Santarossa Aug 28 '16 at 13:51
  • @NathanTrojanaudioBridge if it is in another point of the code you have to write again my guard before checking the empy string. Anyway check if the outlets are set – Marco Santarossa Aug 28 '16 at 13:52
0

Thanks for all of the help guys, Marco's answer was the one that I used that fixed all the errors.

guard let userName = userName.text, let userMail = userMail.text, let userPassword = userPassword.text else {
return
} 
Alamofire.request(.POST, urlRequest, parameters: ["X-API-KEY": API_GENERAL_KEY,"username":userName,"email": userMai,"password":userPassword,"profile_photo": self.imageName])
  • 1
    If you want, mark my answer as right, I would appreciate it and then you can delete this answer, thank you – Marco Santarossa Aug 28 '16 at 13:54
  • As an aside, that prevents the crash, but doesn't really tell you which was `nil`, nor why. Have you figured that out? Simply silently preventing the crash doesn't seem like that's enough... – Rob Aug 28 '16 at 13:58