类型抛出函数的无效转换
Invalid conversion from throwing function of type
我正在尝试将我的代码转换为 Swift 2,但我在使用最后一个函数时遇到了问题。我在 task
:
的声明中收到以下错误
Invalid conversion from throwing function of type ... to non throwing type.
func performSearch(searchTerm: String){
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(urlForQuery(searchTerm)!, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
do {
let result: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
}
let results: AnyObject? = result.objectForKey("results")
if let testVar: NSMutableArray = results as? NSMutableArray {
// Uncomment this to print all feeds
//println(testVar)
self.searchedPodcasts.removeAllObjects()
self.searchedPodcasts.addObjectsFromArray(testVar as [AnyObject])
NSNotificationCenter.defaultCenter().postNotificationName("SearchPerformed", object: self)
}
})
task!.resume()
}
我尝试了以下方法,但它也不起作用,这也意味着我出于某种原因无法调用 task.resume:
func performSearch(searchTerm: String){
let session = NSURLSession.sharedSession()
do {
let task = session.dataTaskWithURL(urlForQuery(searchTerm)!, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
do {
let result: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
}
let results: AnyObject? = result.objectForKey("results")
if let testVar: NSMutableArray = results as? NSMutableArray {
// Uncomment this to print all feeds
//println(testVar)
self.searchedPodcasts.removeAllObjects()
self.searchedPodcasts.addObjectsFromArray(testVar as [AnyObject])
NSNotificationCenter.defaultCenter().postNotificationName("SearchPerformed", object: self)
}
})
} catch {
print("error)
}
task!.resume()
}
我该如何解决?
编辑:试过了,但仍然返回相同的错误。
func test(searchTerm: String) {
let session = NSURLSession.sharedSession()
do {
let task = try session.dataTaskWithURL(urlForQuery(searchTerm)!, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
let result: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
})
} catch {
print("hello")
}
}
尝试在您的 do-try-catch
句子中添加一个 catch 块:
do {
let result: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
}
catch {
print("error");
}
您还需要在外部声明 result
变量或在 do
块内移动更多代码。
有关异常处理的更多信息,请参阅 Swift 2 check out this guide。
Apple 在 Swift 2.
中用 ErrorType 替换了 NSError
因此,将您自己对 NSError 的显式使用替换为 ErrorType,并且您不需要其他答案中建议的额外 try catch 块,并且您可以用更少的代码来表达错误处理。
我正在尝试将我的代码转换为 Swift 2,但我在使用最后一个函数时遇到了问题。我在 task
:
Invalid conversion from throwing function of type ... to non throwing type.
func performSearch(searchTerm: String){
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(urlForQuery(searchTerm)!, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
do {
let result: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
}
let results: AnyObject? = result.objectForKey("results")
if let testVar: NSMutableArray = results as? NSMutableArray {
// Uncomment this to print all feeds
//println(testVar)
self.searchedPodcasts.removeAllObjects()
self.searchedPodcasts.addObjectsFromArray(testVar as [AnyObject])
NSNotificationCenter.defaultCenter().postNotificationName("SearchPerformed", object: self)
}
})
task!.resume()
}
我尝试了以下方法,但它也不起作用,这也意味着我出于某种原因无法调用 task.resume:
func performSearch(searchTerm: String){
let session = NSURLSession.sharedSession()
do {
let task = session.dataTaskWithURL(urlForQuery(searchTerm)!, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
do {
let result: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
}
let results: AnyObject? = result.objectForKey("results")
if let testVar: NSMutableArray = results as? NSMutableArray {
// Uncomment this to print all feeds
//println(testVar)
self.searchedPodcasts.removeAllObjects()
self.searchedPodcasts.addObjectsFromArray(testVar as [AnyObject])
NSNotificationCenter.defaultCenter().postNotificationName("SearchPerformed", object: self)
}
})
} catch {
print("error)
}
task!.resume()
}
我该如何解决?
编辑:试过了,但仍然返回相同的错误。
func test(searchTerm: String) {
let session = NSURLSession.sharedSession()
do {
let task = try session.dataTaskWithURL(urlForQuery(searchTerm)!, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
let result: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
})
} catch {
print("hello")
}
}
尝试在您的 do-try-catch
句子中添加一个 catch 块:
do {
let result: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
}
catch {
print("error");
}
您还需要在外部声明 result
变量或在 do
块内移动更多代码。
有关异常处理的更多信息,请参阅 Swift 2 check out this guide。
Apple 在 Swift 2.
中用 ErrorType 替换了 NSError因此,将您自己对 NSError 的显式使用替换为 ErrorType,并且您不需要其他答案中建议的额外 try catch 块,并且您可以用更少的代码来表达错误处理。