bsquared
bsquared

Reputation: 137

Swift 2.0 API Call

I wrote an iOS app in swift, and had to convert it to 2.0 to put it on the app store. The app was working fine before, but had some migration issues with a JSON API call. I added in a catch statement, but I still keep getting errors. Right now it is saying i'm missing an expression at the end, nothing very descriptive. Any help would be appreciated.

Here is my code:

let url : String = "http://xproshowcasex.api.channel.livestream.com/2.0/livestatus.json"
    let request : NSMutableURLRequest = NSMutableURLRequest()
    request.URL = NSURL(string: url)
    request.HTTPMethod = "GET"

    NSURLSession.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse?, data: NSData?, error: NSError?) -> Void in

        let jsonResult: AnyObject?
        do {
            jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: [])
            print(jsonResult, terminator: "");
            let channel: NSDictionary! = jsonResult!.valueForKey("channel") as! NSDictionary;
            print(channel, terminator: "");
            let result: NSNumber! = channel.valueForKey("isLive") as! NSNumber;
            print(result, terminator: "");

            if (result == 0)
            {
                let alertController = UIAlertController(title: "", message:
                    "There is no live stream right now", preferredStyle: UIAlertControllerStyle.Alert)
                alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))

                self.presentViewController(alertController, animated: true, completion: nil)
            }

        }
         catch
         {
            // TODO: handle
        }
        )
    }
}

Upvotes: 0

Views: 227

Answers (2)

Imran
Imran

Reputation: 2941

You need to replace your

NSURLSession.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse?, data: NSData?, error: NSError?) -> Void in

with

let session = NSURLSession.sharedSession()
        session.dataTaskWithRequest(request) { (data, response, error) -> Void in

        }

Upvotes: 2

Scott H
Scott H

Reputation: 1529

I don't know where sendAsynchronousRequest is defined for NSURLSession, so I can't check the method signature to make sure you are using it correctly, but to start you have a bracket in the wrong spot towards the end.

For what it is worth, in the future, that sort of "expected expression" error is a syntax error and points to something that you didn't type the way the compiler expected. In those cases, double check things like braces and parentheses and other common areas of syntax confusion.

let url : String = "http://xproshowcasex.api.channel.livestream.com/2.0/livestatus.json"
let request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: url)
request.HTTPMethod = "GET"


NSURLSession.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse?, data: NSData?, error: NSError?) -> Void in

    let jsonResult: AnyObject?
    do {
        jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: [])
        print(jsonResult, terminator: "");
        let channel: NSDictionary! = jsonResult!.valueForKey("channel") as! NSDictionary;
        print(channel, terminator: "");
        let result: NSNumber! = channel.valueForKey("isLive") as! NSNumber;
        print(result, terminator: "");

        if (result == 0)
        {
            let alertController = UIAlertController(title: "", message: "There is no live stream right now", preferredStyle: UIAlertControllerStyle.Alert)
            alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))

            self.presentViewController(alertController, animated: true, completion: nil)
        }

    }
    catch
    {
        // TODO: handle
    }
})

Upvotes: 3

Related Questions