class OAuthToken: NSObject,NSCoding { var refreshToken: String? var accessToken: String? var scope: String? convenience init?(refreshToken: String?,accessToken: String?,scope:String) { self.init() if let acutalRefreshToken = refreshToken as String? { self.refreshToken = acutalRefreshToken } else { return nil } if let actualAccessToken = accessToken as String? { self.accessToken = actualAccessToken }else { return nil } self.scope = scope } convenience init?(attributes: Dictionary<String,AnyObject>,scope: String) { var aRefreshToken: String! var anAccessToken: String? aRefreshToken = attributes["refresh_token"] as String? anAccessToken = attributes["access_token"] as String? let token = self.init(refreshToken: aRefreshToken,accessToken: anAccessToken,scope: scope) as OAuthToken // () is not convertible to OAuthToken if token != nil { storeInKeyChain() } else { return nil } } }
当你在另一个内部调用嵌套的可用初始化程序时,如何检查nil的可用初始化程序?
let token = self.init(refreshToken:aRefreshToken,accessToken:anAccessToken,scope:scope)想要返回一个type()对象,而不是可转换为我的类.我如何使用此模式并仅将对象存储到钥匙串(如果它实际上已成功创建)?
我认为当你调用一个超类的可用初始化器时,如果它失败则会有一个隐式返回.
事实上,关于Failable Initializers的文档指出:
If the superclass initialization fails because of an empty name value,the entire initialization process fails immediately and no further initialization code is executed