Search code examples
iosjsonswiftalamofire

While profile updation getting error in swift using Alamofire?


I am using Alamofire to update the profile through .GET method. If I use any space in any field between characters on any field its has error fatal error: unexpectedly found nil while unwrapping an Optional value.But the same URL working fine if I test it on Postman, successfully updated. so through application why it is creating error in 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


Solution

  • Need to remove white space from your string then try it.

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