首页 > 解决方案 > 当我尝试在 iOS SWIFT 中调用 api 时,为什么涉及来自另一种语言的特殊字符会导致崩溃?

问题描述

这是罪魁祸首字符串和参数名称=solsssㅗ</p>

 Alamofire.request(todoEndpoint)
            .responseJSON { response in
                // check for errors
                guard response.result.error == nil else {
                    // got an error in getting the data, need to handle it
                    print("error calling GET on \(todoEndpoint)")
                    print(response.result.error!)
                    DispatchQueue.main.async
                        {
                            self.activityView.isHidden = true
                            self.activityView.stopAnimating()
                    }
                    return
                }

所以我在 URL 中得到错误总是无效的。如果我在没有任何特殊字符的情况下使用它,它就可以正常工作。

标签: iosswiftalamofire

解决方案


您可以使用 String url 尝试此操作 -

let urlWithSpecialCharacter = url.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)

推荐阅读