LoginSignup
1

More than 5 years have passed since last update.

Swift2 の NSURLConnection.sendSynchronousRequest にちょっとつまづいた

Posted at

概要

こういうの書いたらエラーでて悩んだ

do {
    var res: AutoreleasingUnsafeMutablePointer<NSURLResponse?>?
    try NSURLConnection.sendSynchronousRequest(req!, returningResponse: res!)
} catch let e as NSError {
}

fatal error: unexpectedly found nil while unwrapping an Optional value

対応

nil でちゃんと初期化して直った
というより ! があるとエラーがでた

do {
    let res: AutoreleasingUnsafeMutablePointer<NSURLResponse?> = nil
    try NSURLConnection.sendSynchronousRequest(req!, returningResponse: res)
} catch let e as NSError {
}


経緯

Swift2 で NSURLConnection.sendSynchronousRequest に関する処理をちょっと書いたら
fatal errorが表示されてしまいしばらく詰んだ
なのに検索かけても↓みたいな例ばっか出てくる

var data: NSData = NSURLConnection.sendSynchronousRequest(req, returningResponse: nil, error: nil)!

もう今は try の時代だよ!
・・・はじめたばっかなのでよく知らないけど(´・ω・`)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1