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?

Thanks a lot!

Code chunk #1

override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
    return attributesList
}

Error for chunk 1

Method does not override any method from its superclass  

Code chunk #2

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

Error for chunk 2

A non-failable initializer cannot chain to failable initializer 'init(coder:)' written with 'init?'

Code chunk #3

 NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in

Error for chunk 3

'(NSURLResponse!, NSData!, NSError!) -> Void' is not convertible to '(NSURLResponse?, NSData?, NSError?) -> Void'

EDIT: Now that number three is fixed (but still deprecated) I get this error underneath for this code:

Code:

var dictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as! NSDictionary

Error:

Extra argument 'error' in call
Echizzle
  • 3,359
  • 5
  • 17
  • 28

1 Answers1

1
  1. layoutAttributesForElementsInRect now returns [UICollectionViewLayoutAttributes]? not [AnyObject]? write:

    override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    }
    
  2. 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)
    }
    
  3. Do not use explicit types in this case. Just write:

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in
    
    }
    
  4. Read about error handling because try! can let down your app :/

    try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
    

but sendAsynchronousRequest was deprecated in iOS 9

Arsen
  • 10,815
  • 2
  • 34
  • 46
  • okay great thanks! But now I have an error below for #3... I edited the code above. – Echizzle Oct 12 '15 at 18:30
  • Great thanks again! However, now it won't get the parse data for the app and says "2015-10-12 11:48:28.682 FiveTest[2456:98960] [Error]: The resource could not be loaded because the App Transport Security policy requires the use of a secure connection. (Code: 100, Version: 1.8.2)"... Any idea what that's about? – Echizzle Oct 12 '15 at 18:49
  • @Echizzle yes. you have to use `https` instead of `http` – Arsen Oct 12 '15 at 19:02
  • @Echizzle you need to edit your info plist and add the domain your app needs to access – Leo Dabus Oct 20 '15 at 13:55
  • @Echizzle besides that issue http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Leo Dabus Oct 20 '15 at 13:59