Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从PHP脚本接收“JSON_encode”时,Swift JSON中出现错误。没有它也行_Php_Ios_Json_Swift - Fatal编程技术网

从PHP脚本接收“JSON_encode”时,Swift JSON中出现错误。没有它也行

从PHP脚本接收“JSON_encode”时,Swift JSON中出现错误。没有它也行,php,ios,json,swift,Php,Ios,Json,Swift,我有一个按钮的Swift函数,当按下该按钮时,会通过PHP将一些详细信息写入数据库: @IBAction func createCommunityButtonTapped(_ sender: AnyObject) { let communityName = communityNameTextField.text; if (communityName!.isEmpty){ displayMyAlertMessage(userMessage: "You must

我有一个按钮的Swift函数,当按下该按钮时,会通过PHP将一些详细信息写入数据库:

 @IBAction func createCommunityButtonTapped(_ sender: AnyObject) {

    let communityName = communityNameTextField.text;
    if (communityName!.isEmpty){
        displayMyAlertMessage(userMessage: "You must name your Community");

        return;
    }else{

        func generateRandomStringWithLength(length: Int) -> String {

            var randomString = ""
            let letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

            for _ in 1...length {
                let randomIndex  = Int(arc4random_uniform(UInt32(letters.characters.count)))
                let a = letters.index(letters.startIndex, offsetBy: randomIndex)
                randomString +=  String(letters[a])
            }

            return randomString
        }

        let communityCode = generateRandomStringWithLength(length: 6)
        passwordTextField.text = communityCode

        let myUrl = URL(string: "http://www.quasisquest.uk/KeepScore/createCommunity.php?");

        var request = URLRequest(url:myUrl!);
        request.httpMethod = "POST";

        let postString = "communityname=\(communityName!)&code=\(communityCode)&email=\(myEmail!)";

        request.httpBody = postString.data(using: String.Encoding.utf8);

        let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
        if (try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]) != nil {
            }
        } 

        task.resume()

    }

}
除了我在PHP脚本中添加此echo jsonline外,该函数工作得非常好:

if($newresult)
{
    $returnValue["status"] = "Success";
    $returnValue["message"] = "Community is registered";
    echo json_encode($returnValue);
    return;
}
然后我在这一行得到一个错误线程8:EXC_BAD_指令代码=EXC_I386_INVOP,沉降=0x0:

如果尝试!JSONSerialization.jsonObjectwith:data!,选项:。是否允许分段为?[字符串:AnyObject]!=零{ }

在调试区域中显示以下详细信息

数据?一些 回应?0x0000618000223500 错误?无

我想我遗漏了一行,或者需要为JSONSerialization设置一个变量,而不是“try!”但是我很不确定是什么。

您正在返回null。试试这个

if($newresult)
{
 $returnValue["status"] = "Success";
 $returnValue["message"] = "Community is registered";
 return json_encode($returnValue);
}
您正在返回null。试试这个

if($newresult)
{
 $returnValue["status"] = "Success";
 $returnValue["message"] = "Community is registered";
 return json_encode($returnValue);
}