1
私はSwiftの新機能ですが、ログインアプリがユーザー名とパスワードのフィールドをphpファイルに送信し、json応答を返すような例をいくつか挙げました。Swift 3の問題jsonの応答を解析する
私は私のresponseStringを印刷するとき、私は得る:
[{ "USERID":1、 "ユーザ名": "ロドリゴ"、 "パスワード": "minhoca"、 "グループ名": "カップル"}]
しかし、私は、コードのその部分に入り込むことがないので、私は、ユーザー名変数を設定することはできませんでしJSONを解析しようとしたとき、私は「ここ」ヘルプ
func sendLoginInfo(username: String, password: String) -> String{
if let url = URL(string: "myphpurl"){
let request = NSMutableURLRequest(url:url)
request.httpMethod = "POST";// Compose a query string
let postString = "?username=\(myUsername)&password=\(myPassword)"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with:request as URLRequest){
data, response, error in
if error != nil{
print("1\(error)")
}
else{
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("response string = \(responseString!)")
}
do {
if let convertedJsonIntoDict = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary {
// Print out dictionary
print(convertedJsonIntoDict)
// Get value by key
let firstNameValue = convertedJsonIntoDict["username"] as? String
print("here = \(firstNameValue!)")
}
else{
print("here")
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
task.resume()
}
return ""
}
おかげだが、今では私を与える: –
申し訳ありませんが、私はコードのタイプミスをしたNSDictionaryの宣言されていないタイプの 使用。このコードをもう一度試してください私はそれを編集しました。私はそれがNSDictionaryであることを間違って綴られたキーワード –
ありがとう、それは正常に働いた。 –