0

This is how the code looks like right now:

func fetchSalahtimes (){


    let url = NSURL(string: "http://irvingmasjid.org/salah2016.php")
    let request = NSURLRequest(URL: url!)
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config)

    let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in

        if data != nil{ // is this how i should do it to check for success/fail/errors? 



        }

    });

    // do whatever you need with the task e.g. run
    task.resume()

}

I have searched in SO or in google from similar questions, but i could not find something that really suited me. I came this far by searching.

I am trying to grasp how to work with completion handlers. The code is supposed to fetch some json content from the web. And this is where i need help with some questions.

1) If i am going to call this function from another class, how can i retrieve the content that it fetched by using this completion handler?

2) How and where should i proceed to write text to actually get the content? every time i write this to get the JSON content:

    if data != nil{

        let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)

        let sunrise = json["sunrise"] as! String

        print(sunrise)

        salahTimes.append(sunrise)
    }

I get this error message:

Invalid conversion from throwing function of type '(_, _, _) throws -> ()' to non-throwing function type '(NSData?, NSURLResponse?, NSError?) -> Void'
Timo Cengiz
  • 3,367
  • 4
  • 23
  • 45

0 Answers0