Protocol extension to struct and overwritable
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
}
swift properties static override protocols
add a comment |
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
}
swift properties static override protocols
You forget to includeDateFormatProtocol
declaration that could help the readers to provide a better fix..
– Kamran
Nov 22 at 22:07
You're right, I edited the question
– Matías Contreras Selman
Nov 26 at 12:46
Please check the updated answer.
– Kamran
Nov 26 at 12:50
Is there a way that, when implementing the Protocol CodableWithDateFormat the compiler will force the struct to have dateFormat property?
– Matías Contreras Selman
Nov 26 at 14:15
Actually if you remove the default implementation from extension it will force then. Or if you use DateFormateProtocol then it is possible.
– Kamran
Nov 26 at 15:23
add a comment |
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
}
swift properties static override protocols
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
}
swift properties static override protocols
swift properties static override protocols
edited Nov 26 at 12:45
asked Nov 22 at 21:21
Matías Contreras Selman
15518
15518
You forget to includeDateFormatProtocol
declaration that could help the readers to provide a better fix..
– Kamran
Nov 22 at 22:07
You're right, I edited the question
– Matías Contreras Selman
Nov 26 at 12:46
Please check the updated answer.
– Kamran
Nov 26 at 12:50
Is there a way that, when implementing the Protocol CodableWithDateFormat the compiler will force the struct to have dateFormat property?
– Matías Contreras Selman
Nov 26 at 14:15
Actually if you remove the default implementation from extension it will force then. Or if you use DateFormateProtocol then it is possible.
– Kamran
Nov 26 at 15:23
add a comment |
You forget to includeDateFormatProtocol
declaration that could help the readers to provide a better fix..
– Kamran
Nov 22 at 22:07
You're right, I edited the question
– Matías Contreras Selman
Nov 26 at 12:46
Please check the updated answer.
– Kamran
Nov 26 at 12:50
Is there a way that, when implementing the Protocol CodableWithDateFormat the compiler will force the struct to have dateFormat property?
– Matías Contreras Selman
Nov 26 at 14:15
Actually if you remove the default implementation from extension it will force then. Or if you use DateFormateProtocol then it is possible.
– Kamran
Nov 26 at 15:23
You forget to include
DateFormatProtocol
declaration that could help the readers to provide a better fix..– Kamran
Nov 22 at 22:07
You forget to include
DateFormatProtocol
declaration that could help the readers to provide a better fix..– Kamran
Nov 22 at 22:07
You're right, I edited the question
– Matías Contreras Selman
Nov 26 at 12:46
You're right, I edited the question
– Matías Contreras Selman
Nov 26 at 12:46
Please check the updated answer.
– Kamran
Nov 26 at 12:50
Please check the updated answer.
– Kamran
Nov 26 at 12:50
Is there a way that, when implementing the Protocol CodableWithDateFormat the compiler will force the struct to have dateFormat property?
– Matías Contreras Selman
Nov 26 at 14:15
Is there a way that, when implementing the Protocol CodableWithDateFormat the compiler will force the struct to have dateFormat property?
– Matías Contreras Selman
Nov 26 at 14:15
Actually if you remove the default implementation from extension it will force then. Or if you use DateFormateProtocol then it is possible.
– Kamran
Nov 26 at 15:23
Actually if you remove the default implementation from extension it will force then. Or if you use DateFormateProtocol then it is possible.
– Kamran
Nov 26 at 15:23
add a comment |
1 Answer
1
active
oldest
votes
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
}
Man this really helped me. Thanks
– Matías Contreras Selman
Nov 26 at 13:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438110%2fprotocol-extension-to-struct-and-overwritable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
}
Man this really helped me. Thanks
– Matías Contreras Selman
Nov 26 at 13:52
add a comment |
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
}
Man this really helped me. Thanks
– Matías Contreras Selman
Nov 26 at 13:52
add a comment |
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
}
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
}
edited Nov 26 at 12:56
answered Nov 22 at 22:13
Kamran
6,19711028
6,19711028
Man this really helped me. Thanks
– Matías Contreras Selman
Nov 26 at 13:52
add a comment |
Man this really helped me. Thanks
– Matías Contreras Selman
Nov 26 at 13:52
Man this really helped me. Thanks
– Matías Contreras Selman
Nov 26 at 13:52
Man this really helped me. Thanks
– Matías Contreras Selman
Nov 26 at 13:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438110%2fprotocol-extension-to-struct-and-overwritable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You forget to include
DateFormatProtocol
declaration that could help the readers to provide a better fix..– Kamran
Nov 22 at 22:07
You're right, I edited the question
– Matías Contreras Selman
Nov 26 at 12:46
Please check the updated answer.
– Kamran
Nov 26 at 12:50
Is there a way that, when implementing the Protocol CodableWithDateFormat the compiler will force the struct to have dateFormat property?
– Matías Contreras Selman
Nov 26 at 14:15
Actually if you remove the default implementation from extension it will force then. Or if you use DateFormateProtocol then it is possible.
– Kamran
Nov 26 at 15:23