質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

4363閲覧

【swift】dataTask(with: request, completionHandler: でエラー

iphonex

総合スコア30

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2016/12/04 05:54

swift2で書かれた下記コードをswift3でビルド使用とすると、
dataTask(with: request, completionHandler: の部分で

「Cannot invoke 'dataTask' with an argument list of type '(with: NSMutableURLRequest, completionHandler: (Data?, URLResponse?, NSError?) -> ())'」

エラーが発生します。swift3の書き方を教えてください。

class RegisterVC: UIViewController {

@IBOutlet var usernameTxt: UITextField! @IBOutlet var passwordTxt: UITextField! override func viewDidLoad() { super.viewDidLoad() } @IBAction func register_click(_ sender: AnyObject) { let url = URL(string: "http://localhost/register.php")! let request = NSMutableURLRequest(url: url) request.httpMethod = "POST" let body = "username=\(usernameTxt.text!.lowercased())&password=\(passwordTxt.text!)" request.httpBody = body.data(using: String.Encoding.utf8) URLSession.shared.dataTask(with: request, completionHandler: { (data:Data?, response:URLResponse?, error:NSError?) in ↑ エラーはこの部分です。 if error == nil { DispatchQueue.main.async(execute: { do { // get json result let json = try JSONSerialization.jsonObject(with: Data!, options: .mutableContainers) as? NSDictionary // assign json to new var parseJSON in guard/secured way guard let parseJSON = json else { print("データエラー") return } let id = parseJSON["id"] // successfully registered if id != nil { UserDefaults.standard.set(parseJSON, forKey: "parseJSON") user = UserDefaults.standard.value(forKey: "parseJSON") as? NSDictionary DispatchQueue.main.async(execute: { appDelegate.login() }) // error } else { // get main queue to communicate back to user DispatchQueue.main.async(execute: { let message = parseJSON["message"] as! String appDelegate.infoView(message: message, color: colorSmoothRed) }) return } } catch { // get main queue to communicate back to user DispatchQueue.main.async(execute: { let message = String(error) appDelegate.infoView(message: message, color: colorSmoothRed) }) return } }) // if unable to proceed request } else { // get main queue to communicate back to user DispatchQueue.main.async(execute: { let message = error!.localizedDescription appDelegate.infoView(message: message, color: colorSmoothRed) }) return } // launch prepared session }).resume() }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

対象のエラーは解決しましたが、そちらで定義している部分でエラーになっています。
そちらでは動くと思うので試してみてください。

修正は以下のサイトを参考にしました。
NSMutableURLRequestNSURLRequestにキャストする必要があるようです。
Swift 3 URLSession.shared() Ambiguous reference to member 'dataTask(with:completionHandler:) error (bug)

swift

1class RegisterVC: UIViewController { 2 3 @IBOutlet var usernameTxt: UITextField! 4 @IBOutlet var passwordTxt: UITextField! 5 6 override func viewDidLoad() { 7 super.viewDidLoad() 8 } 9 10 @IBAction func register_click(_ sender: AnyObject) { 11 12 let url = URL(string: "http://localhost/register.php")! 13 let request = NSMutableURLRequest(url: url) 14 request.httpMethod = "POST" 15 let body = "username=\(usernameTxt.text!.lowercased())&password=\(passwordTxt.text!)" 16 request.httpBody = body.data(using: String.Encoding.utf8) 17 18 19 URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in 20 21 if error == nil { 22 23 DispatchQueue.main.async(execute: { 24 25 do { 26 // get json result 27 let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary 28 29 // assign json to new var parseJSON in guard/secured way 30 guard let parseJSON = json else { 31 print("データエラー") 32 return 33 } 34 35 let id = parseJSON["id"] 36 37 // successfully registered 38 if id != nil { 39 40 UserDefaults.standard.set(parseJSON, forKey: "parseJSON") 41 user = UserDefaults.standard.value(forKey: "parseJSON") as? NSDictionary 42 43 DispatchQueue.main.async(execute: { 44 appDelegate.login() 45 }) 46 47 // error 48 } else { 49 50 // get main queue to communicate back to user 51 DispatchQueue.main.async(execute: { 52 let message = parseJSON["message"] as! String 53 appDelegate.infoView(message: message, color: colorSmoothRed) 54 }) 55 return 56 57 } 58 59 } catch { 60 61 // get main queue to communicate back to user 62 DispatchQueue.main.async(execute: { 63 let message = String(describing: error) 64 appDelegate.infoView(message: message, color: colorSmoothRed) 65 }) 66 return 67 68 } 69 70 }) 71 72 // if unable to proceed request 73 } else { 74 75 // get main queue to communicate back to user 76 DispatchQueue.main.async(execute: { 77 let message = error!.localizedDescription 78 appDelegate.infoView(message: message, color: colorSmoothRed) 79 }) 80 return 81 } 82 83 // launch prepared session 84 85 }).resume() 86 87 } 88}

投稿2016/12/04 06:39

_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

iphonex

2016/12/04 07:10

エラーメッセージが見事に消えました。ありがとうございます。感謝です。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問