我当时正在学习教程(https://www.simplifiedios.net/swift-php-mysql-tutorial/),但最终陷入困境。
我不断收到此错误:
2018-11-10 19:34:31.931565-0500小组[85540:1441900] [MC]从私人有效用户设置中读取。 错误域= NSCocoaErrorDomain代码= 3840“无值。” UserInfo = {NSDebugDescription =无值。}
这是我的代码:
import UIKit
class ViewController: UIViewController {
let URL_SAVE_TEAM = "http://cgi.soic.indiana.edu/~kim882/journey/createteam.php"
@IBOutlet weak var textFieldName: UITextField!
@IBOutlet weak var textFieldMember: UITextField!
@IBAction func Login(_ sender: UIButton) {
//created NSURL
let requestURL = URL(string: URL_SAVE_TEAM)
//creating NSMutableURLRequest
let request = NSMutableURLRequest(url: requestURL!)
//setting the method to post
request.httpMethod = "POST"
//getting values from text fields
let teamName = textFieldName.text
let memberCount = textFieldMember.text
//creating the post parameter by concatenating the keys and values from text field
let postParameters = "name="+teamName!+"&member="+memberCount!;
//adding the parameters to request body
request.httpBody = postParameters.data(using: String.Encoding.utf8)
//creating a task to send the post request
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error is \(String(describing: error))")
return
}
do { //parsing the response
let myJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary //converting resonse to NSDictionary
//parsing the json
if let parseJSON = myJSON {
//creating a string
var msg : String!
//getting the json response
msg = parseJSON["message"] as! String?
//printing the response
print(msg)
}
} catch {
print(error)
}
}
//executing the task
task.resume()
}
}