gpt4 book ai didi

ios - Swift 2.0 API 调用

转载 作者:行者123 更新时间:2023-11-28 09:58:31 24 4
gpt4 key购买 nike

我用 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定义的,所以我无法检查方法签名来确保你正确使用它,但是开始你在最后的错误位置有一个括号。

就其值(value)而言,在未来,这种“预期表达式”错误是一种语法错误,它指向您没有按照编译器预期的方式键入的内容。在这些情况下,仔细检查大括号和圆括号以及其他常见的语法混淆区域。

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

关于ios - Swift 2.0 API 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32982380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com