如何将URL构建代码从Objective-C转换为Swift?

如何将URL构建代码从Objective-C转换为Swift?,objective-c,swift,Objective C,Swift,我有Objective-C代码来构建URL。如何使用Swift 3代码编写 NSString *urlString = [[NSString alloc]initWithFormat:@"http://smartbaba.in/Familynk/api/registration.php?phone_no=%@&email=%@&password=%@&first_name=%@&last_name=%@",txtmobileno.text,txtemail.tex

我有Objective-C代码来构建URL。如何使用Swift 3代码编写

NSString *urlString = [[NSString alloc]initWithFormat:@"http://smartbaba.in/Familynk/api/registration.php?phone_no=%@&email=%@&password=%@&first_name=%@&last_name=%@",txtmobileno.text,txtemail.text,txtpassword.text,txtfirstname.text,txtlastname.text];
试试这个

"http://smartbaba.in/Familynk/api/registration.php?phone_no=\(txtmobileno.text)&email=\(txtemail.text)&password=\(txtpassword.text)&first_name=\(txtfirstname.text)&last_name=\(txtlastname.text)"

快速转换可以是这样的

// I thing code requires no explanation, its self explanatory
func makeUrl(phoneNo: String, email: String, password: String, firstName: String, lastName: String) {
// first way but not recomended
let urlString = "http://smartbaba.in/Familynk/api/registration.php?phone_no=\(phoneNo)&email=\(email)&password=\(password)&first_name=\(firstName)&last_name=\(lastName)"
print("\(urlString)")

// second way, ASAIK this is good way for constructing URL's
var components = URLComponents()
components.scheme = "http"
components.host = "smartbaba.in"
components.path = "/Familynk/api/registration.php"
components.queryItems = [
    URLQueryItem(name: "phone_no", value: phoneNo),
    URLQueryItem(name: "email", value: email),
    URLQueryItem(name: "password", value: password),
    URLQueryItem(name: "first_name", value: firstName),
    URLQueryItem(name: "last_name", value: lastName)
]
let url = components.url
print(url) // returns URL
print(url?.absoluteString) // returns url path in string

  }
// call function
 makeUrl(phoneNo: "12345", email: "test@gmail.com", password: "12345678", firstName: "test", lastName: "user")

使用URLSession的post方法:

 let myUrl = URL(string: "http://smartbaba.in/Familynk/api/registration.php");

        var request = URLRequest(url:myUrl!)

        request.httpMethod = "POST"

        let postString = "firstName=James&lastName=Bond";

        request.httpBody = postString.data(using: String.Encoding.utf8);

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

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

            // You can print out response object
            print("response = \(response)")

            do {
                let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

                if let parseJSON = json {
                    print(parseJSON)
                }
            } catch {
                print(error)
            }
        }
        task.resume()
如果您使用的是Alamofire

 let parameters: Parameters = [
            "Subject": "hallo"
        ]

  let url = "http://mydomain/mydb/mydb_tesT.NSF/api/data/documents/unid/DD026770D91AA23DC1257EF90035E1C4"

  Alamofire.request(url, method:.post, parameters:parameters, headers:headers).responseJSON { response in
        switch response.result {
        case .success:
            debugPrint(response)

        case .failure(let error):
            print(error)
        }

    } 
您可以简单地使用字符串插值创建具有多个参数的字符串,如

let urlString = "http://smartbaba.in/Familynk/api/registration.php?phone_no=\(txtmobileno.text)&email=\(txtemail.text)&password=\(txtpassword.text)&first_name=\(txtfirstname.text)&last_name=\(txtlastname.text)"
现在,要使用urlString获取URL


http://smartbaba.in/Familynk/api/registration.php?phone_no=\txtmobileno.text&email=\txtmail.text&password=\txtpassword.text&first\u name=\txtfirstname.text&last\u name=\txtlastname.text或者您可以使用Stringformat:\但代码浮动的内容不会使用stringWithFormat构建URL。在Objective-C中也使用相同的url组件。您应该询问Swift 5,而不是Swift 3。我的问题是Swift参数中的post方法如何包含在url中。听起来您需要用更多相关信息来回答您的问题。确保您发布了您尝试过的Swift代码,并清楚地解释了您遇到的问题以及您需要的帮助。仅仅发布一个非常模糊的需求对于堆栈溢出问题来说是不够的。
if let url = URL(string: urlString) {
    //use url here...
}