xcode Swift - 每个类都需要 XCode6 beta5 init(coder)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25177801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift - XCode6 beta5 required init(coder) for every class
提问by Kosmetika
In latest XCode 6 beta (5) I noticed that almost every class in my app complains with error:
在最新的 XCode 6 beta (5) 中,我注意到我的应用程序中几乎每个类都抱怨错误:
Class does not implement its superclass's required members
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?
问题是为什么它甚至与 AFNetworking 的 AFHTTPSessionManager 的子类相关?我错过了什么吗?
回答by Kirsteins
Because AFHTTPSessionManager
conforms to NSCoding
and initWithCoder:
is required. From manual:
因为AFHTTPSessionManager
符合NSCoding
并且initWithCoder:
是必需的。从手册:
initWithCoder: Returns an object initialized from data in a given unarchiver. (required)
initWithCoder:返回从给定解压缩器中的数据初始化的对象。(必需的)
回答by newacct
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.
因为您的类覆盖了超类的一些指定初始化程序,所以它不会自动从超类继承初始化程序。如果您没有覆盖任何初始化器,那么来自超类的所有初始化器都将被自动继承,从而满足 NSCoding 所需的初始化器。