iOS 9错误并正确转换为Swift 2 [英] iOS 9 errors and correct conversion to swift 2
问题描述
因此,在更新到iOS 9和Swift 2之后,我的项目中出现了很多错误,即使单击自动转换"选项后也无法解决.幸运的是,大多数都非常简单,我能够弄清楚它们,但是我还剩下三个主要的.有谁能帮助我解决这些问题?
So after updating to iOS 9 and Swift 2 I had many errors in my project that even after clicking the convert automatically option didn't get fixed. Luckily most were pretty simple and I was able to figure them out but I have three major ones left. Is anyone able to help me out with these?
非常感谢!
代码块#1
override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
return attributesList
}
第1块错误
Method does not override any method from its superclass
代码块#2
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
第2块错误
A non-failable initializer cannot chain to failable initializer 'init(coder:)' written with 'init?'
代码块#3
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
第3块错误
'(NSURLResponse!, NSData!, NSError!) -> Void' is not convertible to '(NSURLResponse?, NSData?, NSError?) -> Void'
现在,第三个数字是固定的(但仍然不建议使用),我在此代码的下面看到此错误:
Now that number three is fixed (but still deprecated) I get this error underneath for this code:
代码:
var dictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as! NSDictionary
错误:
Extra argument 'error' in call
推荐答案
-
layoutAttributesForElementsInRect
现在返回[UICollectionViewLayoutAttributes]?
而不是[AnyObject]?
写入:
layoutAttributesForElementsInRect
now returns[UICollectionViewLayoutAttributes]?
not[AnyObject]?
write:
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
}
Super.init现在是容易犯错的public init?(coder aDecoder: NSCoder)
,这意味着它可能返回nil,所以您必须编写:
Super.init now is fallible public init?(coder aDecoder: NSCoder)
it means It may return nil so you have to write:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
在这种情况下,请不要使用显式类型.只需写:
Do not use explicit types in this case. Just write:
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in
}
阅读有关错误处理的信息,因为try!
可能会使您的应用程序失望:/
Read about error handling because try!
can let down your app :/
try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
但 sendAsynchronousRequest在iOS 9中已弃用
这篇关于iOS 9错误并正确转换为Swift 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!