Reputation:
I have this struct and I want its init
to be failable
because something could go wrong with the JSON dictionary I pass:
struct AdminModel:Interface{
var status:Any = ""
var l:String = ""
var p:String = ""
var url:String = ""
init?(json:NSDictionary){
if let status = json["status"] as? Any,
let l = json["l"] as? String,
let p = json["p"] as? String,
let url = json["url"] as? String
{
self.status = status
self.l = l
self.p = p
self.url = url
}else{
return nil
}
}
}
There's no issue until I add ?
after init to make init failable
: at that point XCode complains:
Non-failable initializer requirement 'init(json:)' cannot be satisfied by failable initializer ('init?')
Why my struct can't be failable? Should I declare failable even the protocol init?
Upvotes: 2
Views: 1902
Reputation: 607
Maybe your Interface
is like:
protocol Interface {
init(json: JSON)
}
but your AdminModel's init is init?(json: JSON)
, so you should keep consistent:
protocol Interface {
init?(json: JSON)
}
Upvotes: 0