-1

When i am calling api with Alamofire from iOS getting error -

Object reference not set to an instance of an object

All other apis are working fine. Don't know it is from backend or not.

I debug it and check log with po command, but all parameters are going with request.

Can anyone suggest a solution ?

Any quick help will be appreciated.

Thanks

Code is given below:

let strToken: String = (USER_DEFAULT.value(forKey: "token") as? String)!
    let strQuerystring = "token=" + strToken
    let parameters: [String: String] = ["FirstName": txtFirstName.text!, "LastName":txtLastName.text!,"Gender":txtGender.text!,"DOB":strDate,"RelationId":strMemberID]

    callPostApi(fileName: postAddMember + strQuerystring, parameters: parameters) { responseObject, errorResponse in
        if(errorResponse == nil)
        {
            if let json = responseObject as? NSDictionary
            {
                let strStatus: Bool = json.value(forKey: "Status") as! Bool
                let strMessage: String = json.value(forKey: "Message") as! String
                if(strStatus == true)
                {
                    forAlert(strMessage)
                    _ = self.navigationController?.popViewController(animated: true)
                }
                else
                {
                    forAlert(strMessage)
                }
            }
            else
            {
                forAlert("noResponseMessage".localized)
            }
        }
        else
        {
            forAlert("noResponseMessage".localized)
        }
        SKActivityIndicator.dismiss()
    }
}
Scriptable
  • 19,402
  • 5
  • 56
  • 72
jaydeep darji
  • 45
  • 1
  • 8
  • can you please some sort of code so it will be better to understand – Parth Dhorda Jan 29 '19 at 11:41
  • sounds like this is coming from your backend, probably a .Net API. You need to check your params, API documentation etc. You could use some other tool such as Postman (chrome app) to make the same API request and test it. if you get the same error it's nothing to do with iOS – Scriptable Jan 29 '19 at 11:44
  • @Scriptable - thanks for your reply. I check all the things from swift side. This is api is working fine with PostMan. Also i discussed this with backend developer. He is denying that he has not changed anything. Are you sure it is not from swift side ? – jaydeep darji Jan 29 '19 at 11:47
  • 1
    if the error is in `strMessage` it's backend. if app crashes and error is in console, it's crashing in iOS, which could still be because something changed on API. "Object reference not set to an instance of an object" is a common error in C# and probably other languages. if it was in Swift it would be `unexpectedly found nil ` – Scriptable Jan 29 '19 at 11:49
  • 1
    if it works fine in Postman, then your either sending the wrong params, wrong values, or sending in the wrong format – Scriptable Jan 29 '19 at 12:02
  • @Scriptable he has made a wrong declaration of params variable – Enea Dume Feb 05 '19 at 10:35

3 Answers3

1

The response object is not json, you will need to serialize it first before assigning the values to their respective variables. something like this :

guard let dictionary = try? JSONSerialization.jsonObject(with:responseObject!,options: []) as! [String: AnyObject] else { return }

Hope that works.

Enea Dume
  • 3,014
  • 3
  • 21
  • 36
Ali Ysf
  • 148
  • 11
1

You need to conform URLRequest with your parameters in body as Data

and create the request as following :

    let auth = "Bearer"+token // or your Authorization type
    let url = URL.init(string:urlString)
    var request = URLRequest(url: url!)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue(auth, forHTTPHeaderField: "Authorization")
    request.httpBody = try! JSONSerialization.data(withJSONObject: parameters)
    Alamofire.request(request)
           .responseJSON { response in
            // Handling your data 
    }

try this and hope that help you .

you can see : How to post nested json by SwiftyJson and Alamofire?

see also : Using manager.request with POST

taha
  • 769
  • 6
  • 5
0

You don't need to serialize the parameters. From Alamofire Docs parameters must be of type Parameters

let parameters: Parameters = [
    "foo": "bar",
    "baz": ["a", 1],
    "qux": [
        "x": 1,
        "y": 2,
        "z": 3
    ]
]

In your case import Alamofire and then declare parameters like this and do not serialize it.:

 let parameters: Parameters = [
        "FirstName": txtFirstName.text!,
        "LastName":txtLastName.text!,
        "Gender":txtGender.text!,
        "DOB":strDate,
        "RelationId":strMemberID
    ]
Enea Dume
  • 3,014
  • 3
  • 21
  • 36