【问题标题】:While profile updation getting error in swift using Alamofire?使用 Alamofire 快速更新个人资料时出错?
【发布时间】:2016-08-31 10:06:01
【问题描述】:

我正在使用 Alamofire 通过 .GET 方法更新配置文件。如果我在任何字段的字符之间的任何字段中使用任何空格,则它会出现错误fatal error: unexpectedly found nil while unwrapping an Optional value。但是如果我在 Postman 上测试它,相同的 URL 工作正常,成功更新。所以通过应用程序为什么它在 Alamofire 中产生错误。

 @IBAction func btnSavePressed(sender: AnyObject) {

    Firstname = txtFName.text!
    Lastname = txtLname.text!
    Username = txtUname.text!
    Phone = Int(txtPhone.text!)
    DoorNo = txtDoorNo.text!
    Street = txtStreet.text!
    Town = txtTown.text!
    Postcode = Int(txtPostCode.text!)

    print(Firstname)
    print(Lastname)
    print(Username)
    print(Phone)
    print(DoorNo)
    print(Street)
    print(Town)
    print(Postcode)
    print("http://\(platform).eposapi.co.uk/?app_id=\(apiID)&app_key=\(apikey)&request=\(request)&id=\(UserID)&fname=\(Firstname)&lname=\(Lastname)&phone=\(Phone)&dno=\(DoorNo)&add1=\(Street)&add2=\(Town)&postcode=\(Postcode)")

    UPDATE_data_from_URl()
}

 func UPDATE_data_from_URl(){

    Alamofire.request(.GET, "http://\(platform).eposapi.co.uk/?app_id=\(apiID)&app_key=\(apikey)&request=\(request)&id=\(UserID)&fname=\(Firstname)&lname=\(Lastname)&phone=\(Phone)&dno=\(DoorNo)&add1=\(Street)&add2=\(Town)&postcode=\(Postcode)", parameters: nil )
        .responseJSON {
            response in
            print(response)

            if let result: AnyObject = response.result.value {

                let post: JSON = JSON(result)

                let action = post["action"].boolValue
                let info = post["info"].stringValue
                print(action)
                print(info)

                if action == false{
                    let alert = UIAlertController(title: "", message: info, preferredStyle: UIAlertControllerStyle.Alert)
                    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
                    self.presentViewController(alert, animated: true, completion: nil)
                }
                else{
                    let alert = UIAlertController(title: "Success", message: info, preferredStyle: UIAlertControllerStyle.Alert)

                    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default){
                        (action) in



                        let sb = UIStoryboard(name: "Main", bundle: nil)
                        let vc = sb.instantiateViewControllerWithIdentifier("yourtabbarvcidentifier") as! UITabBarController
                        vc.selectedIndex = 0
                        self.revealViewController().pushFrontViewController(vc, animated: true)


                        })
                    self.presentViewController(alert, animated: true, completion: nil)

【问题讨论】:

  • 使用其他 API,Alamofire.request(.GET, baseUrl, parameters: yourParamsHere, encoding: .URLEncoded, headers:nil) 这将使 url 等为您提供均匀的空格
  • 但是没有POST方法。所以这里的参数我应该在这里使用什么@Shubhank
  • 我只向您展示了您在问题中拥有的 GET 方法
  • 检查你的valid是否为空
  • 在使用 GET 时如何传递参数。我只有在 POST 方法中传递参数。给点想法@Shubhank

标签: ios json swift alamofire


【解决方案1】:

需要从你的字符串中删除空格然后尝试它。

let linkString = "http://maps.google.com/maps?q=\(Location!)"
let address = NSURL(string:linkString.stringByReplacingOccurrencesOfString(" ", withString: ""))!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    相关资源
    最近更新 更多