Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - XCode6 beta5 required init(coder) for every class [duplicate]

In latest XCode 6 beta (5) I noticed that almost every class in my app complains with error:

Class does not implement its superclass's required members

For example:

import UIKit

let _sharedAPIManager = APIManager(baseURL: NSURL.URLWithString(API_URL))

class APIManager: AFHTTPSessionManager {

class var sharedInstance : APIManager {
    return _sharedAPIManager
}

// this fixes compiler error but why it should be here?
required init(coder aDecoder: NSCoder!) {
    super.init(coder: aDecoder)
}

override init() {
    super.init()
}

override init(baseURL url: NSURL!) {
    super.init(baseURL: url)

    self.responseSerializer = AFJSONResponseSerializer()
    self.requestSerializer = AFJSONRequestSerializer()

    self.requestSerializer.setValue(API_KEY, forHTTPHeaderField: "X-Api-Key")
    self.requestSerializer.setValue("3", forHTTPHeaderField: "X-Api-Version")
}

override init(baseURL url: NSURL!, sessionConfiguration configuration: NSURLSessionConfiguration!) {
    super.init(baseURL: url, sessionConfiguration: configuration)
}

The question is why it's relevant even in subclassing of AFNetworking's AFHTTPSessionManager? Did I missed something?

like image 975
Kosmetika Avatar asked Dec 15 '22 20:12

Kosmetika


2 Answers

Because AFHTTPSessionManager conforms to NSCoding and initWithCoder: is required. From manual:

initWithCoder: Returns an object initialized from data in a given unarchiver. (required)

like image 89
Kirsteins Avatar answered Apr 20 '23 00:04

Kirsteins


Because your class overrides some of the superclass's designated initializers, it does not automatically inherit initializers from the superclass. If you had not overridden any initializers, then all the initializers from the superclass would be automatically inherited, and thus the required initializer from NSCoding would be satisfied.

like image 31
newacct Avatar answered Apr 20 '23 00:04

newacct