当我按下按钮this error appears
时,这是视图控制器这是我从中创建实例的类 创建该实例后,将显示错误 MC从私人有效用户设置中读取。
import UIKit
class authentication: NSObject {
func runCode(password: String,email: String,name: String, completionHandler:@escaping (Bool) ->()){
struct f {
static var flag = false
}
let Points : Int = 0
let passwardV : String = password
let emailV : String = email
let nameV : String = name
// connect data base
var request = URLRequest(url: URL(string: "https://www./////.com/API/register.php")!)
// POST method
request.httpMethod = "POST"
// post senteace
let postString = "Password="+passwardV+"&Email="+emailV+"&Name="+nameV
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
// check for fundamental networking error
print("error=\(error)")
return
}
//parsing the response
do {
//converting resonse to NSDictionary and get JSON result
let myJSON = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary
// receving json result
print(myJSON!)
if(myJSON!["error"] as! Bool == true){
print("alert")
f.flag = false
}
else {
print(myJSON!)
// save enformation in user's divice
let defaults = UserDefaults.standard
defaults.set(nameV, forKey:"Name")
defaults.set(emailV, forKey:"Email")
defaults.set(Points, forKey:"Point")
defaults.synchronize()
//self.shouldPerformSegue(withIdentifier: "TOHomeCustomerVC", sender: self)
f.flag = true
}
} catch let error as NSError {
print(error)
}
}
completionHandler(f.flag)
task.resume()
}
}