gpt4 book ai didi

swift - 调整 Swift 代码以与 Swift 3 配合使用

转载 作者:行者123 更新时间:2023-11-30 12:54:51 27 4
gpt4 key购买 nike

我是 Swift 和 XCode 的新手,但现在只是尝试一下,看看进展如何。

无论如何,我已经看了 this postBelal Khan 的一些代码。将手机应用程序连接到 MySQL 数据库。不过,它似乎需要更新才能与最新版本的 Swift 配合使用。

我想我已经完成了大部分内容,但我一直在更新一个特定部分。

//
// ViewController.swift
// SwiftPHPMySQL
//
// Created by Belal Khan on 12/08/16.
// Copyright © 2016 Belal Khan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

//URL to our web service
let URL_SAVE_TEAM = "http://www.example.com/api/createteam.php"


//TextFields declarations
@IBOutlet weak var textFieldName: UITextField!
@IBOutlet weak var textFieldMember: UITextField!



//Button action method
@IBAction func buttonSave(sender: UIButton) {

//created NSURL
let requestURL = NSURL(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.dataUsingEncoding(NSUTF8StringEncoding)


//creating a task to send the post request
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in

if error != nil{
print("error is \(error)")
return;
}

//parsing the response
do {
//converting resonse to NSDictionary
let myJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? 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()

}


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

这是我迄今为止更新的内容...

//
// ViewController.swift
// SwiftPHPMySQL
//
// Created by Belal Khan on 12/08/16.
// Copyright © 2016 Belal Khan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

//URL to our web service
let URL_SAVE_TEAM = "http://www.example.com/api/createteam.php"


//TextFields declarations
@IBOutlet weak var textFieldName: UITextField!
@IBOutlet weak var textFieldMember: UITextField!



//Button action method
@IBAction func buttonSave(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: .utf8)


//creating a task to send the post request
let task = URLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in

if error != nil{
print("error is \(error)")
return;
}

//parsing the response
do {
//converting resonse to NSDictionary
let myJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? 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()

}


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

错误就行:

let task = URLSession.sharedSession().dataTaskWithRequest(request){

我得到的错误是无法调用非函数类型“URLSession”的值

最佳答案

改成这样

 URLSession.shared.dataTask(with: request, completionHandler: {data, response, error in
...
})

关于swift - 调整 Swift 代码以与 Swift 3 配合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40544604/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com