首页 > 解决方案 > Swift:从 php 解码 NSString

问题描述

我有以下代码,我无法将 responseString 拆分/解码为多个部分。我希望能够将其转换为我可以在整个程序中使用的变量。(第一次发帖,感谢您的耐心等待)。我无法让 responseString 在 {} 括号外工作,也无法在括号内进行 JSON 解码。

    @IBAction func onLogin(_ sender: Any) {

        let loginUserSend = loginUser.text
        let loginPasswordSend = loginPassword.text

    if loginUserSend!.isEmpty {

        loginError.text = "Please input user name"
        loginError.textColor = UIColor.red

    } else if loginPasswordSend!.isEmpty {

        loginError.text = "Please input password"
        loginError.textColor = UIColor.red

} else {

print ("Moving to php request:")

let request = NSMutableURLRequest(url: NSURL(string: "http://www.website.com/IOS/connectionIOSlogin.php")! as URL)
request.httpMethod = "POST"
print("Request: \(request)")

let postString = "loginUser=\(loginUser.text!) "
request.httpBody = postString.data(using: String.Encoding.utf8)
print("postString: \(postString)")

let task = URLSession.shared.dataTask(with: request as URLRequest) {
(data, response, error) in

if error != nil {
    print("error=\(error)")
    return

} // Ends errror If statements
    
    //print("response = \(response)")
    let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
    print("responseString = \(responseString)")
    
}


task.resume()

它返回以下内容:

Moving to php request:
Request: <NSMutableURLRequest: 0x600002bdc180> { URL: http://www.website.com/IOS/connectionIOSlogin.php }
postString: loginUser=Test1
 
2020-09-22 20:34:55.471811-0300 ABCDEF GHIJK[17260:1385975] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed

responseString = Optional(Row :Array
(
    [0] => 1
    [C_Id] => 1
    [1] => Test1
    [C_User] => Test1
    [2] => Test1
    [C_Pword] => Test1
)
Array
(
    [0] => 1
    [C_Id] => 1
    [1] => Test1
    [C_User] => Test1
    [2] => Test1
    [C_Pword] => Test1
)
)

标签: jsonswiftxcodenested

解决方案


推荐阅读