0
let root = "api.site.org" 

@IBAction func signupAccount(_ sender: Any) {
    let q = self.tempemail.text ?? "nil" 
    let w = self.tempusername.text ?? "nil" 
    let e = self.temppassone.text ?? "nil" 
    let r = self.temppasstwo.text ?? "nil"
    if q == "" || w == "" || e == "" {
        print("Please fill in all fields")
    } else {
        if e == r {
            let email = q
            let username = w
            let password = e
            let a = "username="+username
            let b = "&password="+password
            let c = "&email="+email
            let po = a+b+c
            let thingy = URL(string: root+"/signup");

            var request = URLRequest(url:thingy!)

            request.httpMethod = "POST"
            request.httpBody = po.data(using: String.Encoding.utf8);

            let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in

                if error != nil
                {
                    print("error=\(String(describing: error))")
                    return
                }
                do {
                    let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

                    if let parseJSON = json {
                        let Value = parseJSON["success"] as? Bool
                        if Value == true {
                            print("Signup Successful")
                        }
                    }
                } catch {
                    print(error)
                }
            }
            task.resume()
        } else {
            print("Passwords do not match.")
        }
    }
}

Above is the code I am currently using. For some reason on line 42, it triggers this Thread 1: EXC_BREAKPOINT (code=1, subcode=0x101a8fb50) and errors with fatal error: unexpectedly found nil while unwrapping an Optional value.

I'm not sure how to go about fixing this since q,w,e,r are all textfields.

For reference: Line 42: let e = self.temppassone.text ?? "nil"

Any help is very much appreciated. I've been pulling my hair out for quite a while. Not sure why it's "nil" if text is in the textfields when button is pressed. Very odd.

Hamish
  • 78,605
  • 19
  • 187
  • 280
Sanjay
  • 203
  • 1
  • 4
  • 10
  • Is `temppassone` an `IBOutlet`? Is it correctly connected in the storyboard? – Matusalem Marques Oct 09 '17 at 22:44
  • Indeed. I've already checked for this. – Sanjay Oct 09 '17 at 22:50
  • Why are you doing `let q = self.tempemail.text ?? "nil"` instead of `let q = self.tempemail.text ?? ""` What's the point of setting the string to `"nil"` instead of the empty string which you check for on the `if` statement? – rmaddy Oct 10 '17 at 00:12
  • 1
    If `let e = self.temppassone.text ?? "nil" ` is causing the crash then the outlet is *not* connected. – rmaddy Oct 10 '17 at 00:12
  • How are you creating this instance of the view controller? A common cause of this issue is instantiating the view controller via an initialiser instead of through the storyboard resulting in the outlets being `nil` – Paulw11 Oct 10 '17 at 00:27
  • I reconnected all of my outlets and that seem to have fixed it. Thanks so much! – Sanjay Oct 10 '17 at 00:31

1 Answers1

1

The only way that line of code could possibly fail with that error is if temppassone is nil. Put a breakpoint on the line and when execution stops type this in the console: po self.temppassone. I fully expect that the command will return nil. If it doesn't, then double check that the line of code referenced is really the one that is having the problem.

If temppassone is an IBOutlet and is returning nil, then try a clean and rebuild. (command-option-shift-K)

Daniel T.
  • 32,821
  • 6
  • 50
  • 72