Reputation: 500
I want to be able to decode different objects that come from the server with different date formats and for that I came up with this protocol:
public protocol DateFormatProtocol{
var dateFormat : String {get}
}
public protocol CodableWithDateFormat : Codable, DateFormatProtocol{
static var dateFormat: String {get}// = "DatFormat"
}
public extension CodableWithDateFormat{
public static var dateFormat: String { return "Base date format" }
}
So when I need to I could override the property in each struct that needs a different date format but I don't want eavery struct to override the default date format given on the extension of the protocol. Is there any way for me being able to write this? :
struct Struct1 : CodableWithDateFormat{
var dateFormat: String { return "Overwritten Date Format" }
let prop1 : String
let prop2 : Int
}
struct Struct2 : CodableWithDateFormat{ //Type 'Struct2' does not conform to protocol 'DateFormatProtocol'
let prop1 : String
let prop2 : Int
}
Upvotes: 1
Views: 252
Reputation: 15248
You need to match the declaration for dateFormat
in DateFormatProtocol
as below,
public protocol DateFormatProtocol {
static var dateFormat: String { get }
}
public protocol CodableWithDateFormat: Codable, DateFormatProtocol {}
public extension CodableWithDateFormat {
public static var dateFormat: String { return "Base date format" }
}
struct Struct1: CodableWithDateFormat {
public static var dateFormat: String { return "Overwritten Date Format" }
let prop1: String
let prop2: Int
}
struct Struct2: CodableWithDateFormat {
let prop1: String
let prop2: Int
}
Upvotes: 1