1
votes

I have some problem with GET method Alamofire. When I get data I have error:

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) fatal error: unexpectedly found nil while unwrapping an Optional value

What am I doing wrong? My code:

        let URLString = "http://MyURLWebService...";
        Alamofire.request(.GET, URLString)
        .responseJSON { (response) in 
            print(response.request)  // original URL request
            print(response.response) // URL response
            print(response.data)     // server data
            print(response.result)   // result of response serialization

            if let JSON = response.result.value {
                print(JSON["Phones"] as! String)
                //print("JSON: \(JSON)")
            }

    }
2
My format REST API data: [{Phones":"+1 (100) 111-22-33","Phones2":null}]Dim

2 Answers

0
votes

I think the JSON object has no member with "Phones" as name. So it's better to use ? instead !. print(JSON["Phones"] as? String)

Edit:

Your Json string is corrupted. You must wrap Phones with double quotations.

like this:

[{"Phones":"+1 (100) 111-22-33","Phones2":null}]

0
votes

This is work:

            if let jsonResult = response.result.value {
                let Address = jsonResult[0]["Address"]
                let Phones = jsonResult[0]["Phones"]
                print("JSON:    Address: \(Address)")
                print("JSON: Phones: \(Phones)")
            }

I hope it help somebody