Swift 2.0 API 调用

Swift 2.0 API Call

我在 swift 中编写了一个 iOS 应用程序,必须将其转换为 2.0 才能将其放在应用程序商店中。该应用程序之前运行良好,但在调用 JSON API 时遇到了一些迁移问题。我添加了一个 catch 语句,但我仍然不断出错。现在它说我在最后缺少一个表达式,没有什么描述性的。任何帮助将不胜感激。

这是我的代码:

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
        }
        )
    }
}

我不知道 sendAsynchronousRequest 在哪里为 NSURLSession 定义,所以我无法检查方法签名以确保您正确使用它,但首先您有一个括号在最后的错误位置。

为了它的价值,在未来,那种 "expected expression" 错误是语法错误,它指向你没有按照编译器预期的方式键入的内容。在这些情况下,仔细检查大括号和圆括号以及其他常见的语法混淆区域。

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
    }
})

您需要更换

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

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

        }