2016-11-11 5 views
0

私はスウィフトとXコードの初心者です。スウィフトコードをスウィフト3に適合させるために

とにかく、Belal Khanからthis postのコードを見て、電話アプリを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){ 

私が手にエラーがCannot call value of non-function type 'URLSession'

です
+0

あなたが私の答えをチェックすることができ、それが仕事やないですか? –

+0

私は、答えの組み合わせを使ってエラーメッセージを排除することができました。しかし、フォームはまだ私のデータベースに書いていません。 – Tom

答えて

0

これに変更する

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

あなたはNSMutableURLRequestとして要求を作成しているが、、URLSessionはのURLRequest

let request = URLRequest(url: NSURL(string: "") as! URL) 

URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in 
    if error == nil { 
    print("Seccess") 
    } else { 
    print("Error") 
    } 
} 

を必要としてくれてありがとう:)

関連する問題