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.