本文介绍了Swfit 2“额外参数”错误“在通话中”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在理解xcode想要什么,当它给我调用中的额外参数'错误'时它一直有问题它一直指向
I've been having a problem understanding what xcode wants from me when it gives me the "Extra argument 'error' in call" it keeps pointing to
if let feed = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: nil) as? NSDictionary,
我在swift 2中读到的地方我应该添加 do {但每次我添加它我只是不断打破更多的东西。 swift 2中的正确语法是什么?
I read somewhere that in swift 2 I should add do { but everytime I add it I just keep breaking more stuff. What's the correct syntax in swift 2?
这是代码:
override func viewDidLoad() {
super.viewDidLoad()
let request = NSURLRequest(URL: NSURL(string: feedURL)!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { response, data, error in
if let feed = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: nil) as? NSDictionary,
title = feed.valueForKeyPath("feed.entry.im:name.label") as? String,
artist = feed.valueForKeyPath("feed.entry.im:artist.label") as? String,
imageURLs = feed.valueForKeyPath("feed.entry.im:image") as? [NSDictionary] {
if let imageURL = imageURLs.last,
imageURLString = imageURL.valueForKeyPath("label") as? String {
self.loadImageFromURL(NSURL(string:imageURLString)!)
}
self.titleLabel.text = title
self.titleLabel.hidden = false
self.artistLabel.text = artist
self.artistLabel.hidden = false
}
}
}
推荐答案
以下是swift 2中新的错误处理方式...
Here is the new way of error handling in swift 2...
do {
if let feed = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) as? NSDictionary {
// Success block...
}
} catch {
print(error)
}
这篇关于Swfit 2“额外参数”错误“在通话中”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!