-1

I was working on my app and it works fine on simulator. While we I tried that on my iphone, it crashed with unwarpping error.Any ideas? Thx error screenshot

 class connection{
var url: String = ""
func connectServer(url:String,completionHandler: (jsonResponse: JSON) -> ()) {    
    Alamofire.request(.GET, url)
        .responseJSON { (response) in
            let json = JSON(response.result.value!)
            //        print(json) // works fine
            completionHandler(jsonResponse: json)
    }
}

}

 func loadData(){
    let cn = connection()
    let url = "http://localhost:3000/api/v1/patients/user_id/"+userID
    // Call function to connect server by API with specific url
    cn.connectServer(url) { (jsonResponse) in
        //Parsing JSON file
        for item in jsonResponse["patients"].arrayValue{
            //pasring json
            }
            }}
Janice Zhan
  • 571
  • 1
  • 5
  • 18
  • 1
    The reason is that `response.result.value!` is `nil`. Most likely `localhost` is not available on the device. I guess that Alamofire returns also an error object in case of an error. Check that always first! – vadian May 02 '16 at 13:09
  • 1
    You should always consider the possibility that an optional could be nil, and therefore write error handling logic to correctly handle that case – [see this Q&A for ways of safely dealing with optionals.](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Hamish May 02 '16 at 13:10
  • @vadian You are right. Thanks for your help. I tried to print out the error message and said it cannot connect to server. Is there any solution to make my app working on iphone well, i mean still fetching data from localhost? – Janice Zhan May 02 '16 at 13:26
  • Your iphone will not be able to connect to localhost. If you are on the same network, you might be able to get the ip address of your computer and do it that way. – ryantxr May 02 '16 at 13:29

1 Answers1

0

Try this one:

var url: String = ""
    func connectServer(url:String,completionHandler: (jsonResponse: JSON) -> ()) {
        Alamofire.request(.GET, url)
            .responseJSON { (response) -> Void in
                guard response.result.isSuccess else {
                    print("Error while fetching remote rooms: \(response.result.error)")
                    completion(nil)
                    return
                }

                guard let value = response.result.value as? [String: AnyObject],
                    completionHandler(jsonResponse: value) else {
                        print("Malformed data received from fetchAllRooms service")
                        completion(nil)
                        return
                }


        }
    }
Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
  • what does completion(nil)means? I tried that but gives me an unresolved identifier error – Janice Zhan May 02 '16 at 13:21
  • just replace if let value = response.result.value as? [String: AnyObject] { completionHandler(jsonResponse: value) } with your let json = JSON(response.result.value!) – Tejas Ardeshna May 02 '16 at 13:38