使用Alamofire时,配置文件更新在swift中出错?

时间:2016-08-31 10:06:02

标签: ios json swift alamofire

我正在使用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)

enter image description here

1 个答案:

答案 0 :(得分:1)

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

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