次のスニペットのtest1()コンパイルでは、test2()はコンパイルしません。タイプパラメータInt
の変数をリクエストパラメータに代入する方法は?要求パラメータにint変数を割り当てる方法は?
スウィフトバージョン3.1.1(SWIFT-3.1.1-RELEASE) ターゲット:x86_64版 - 未知のlinux-gnuの1がintではないためだ
import Testing
import HTTP
func test1() throws {
let req = Request.makeTest(method: .get)
//OK compiles
req.parameters["id"] = 1
}
func test2() throws {
let req = Request.makeTest(method: .get)
let id = 1
// NOK doesn't compile
// compiler error:
// /path/to/file.swift:12:28: error: cannot assign value of type 'Int' to type 'Parameters?'
// req.parameters["id"] = id
// ^~
req.parameters["id"] = id
}
ジャスト 'req.parameters [ "ID"]を使用してint型= id'。? – AlexSmet
コンパイルされません。 'エラー:オプションの型の値 'Parameters?'アンラップされていない。あなたは '!'を使うつもりでしたか? 'エラー:プロパティに割り当てることができません: 'int'はget-onlyプロパティです。 'または' '?' – gubaer
' req.parameters ["id"] !. int = id'はコンパイルされません。 – gubaer