Protocol extension to struct and overwritable





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















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
}









share|improve this question

























  • You forget to include DateFormatProtocol declaration that could help the readers to provide a better fix..

    – Kamran
    Nov 22 '18 at 22:07











  • You're right, I edited the question

    – Matías Contreras Selman
    Nov 26 '18 at 12:46











  • Please check the updated answer.

    – Kamran
    Nov 26 '18 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 '18 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 '18 at 15:23


















1















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
}









share|improve this question

























  • You forget to include DateFormatProtocol declaration that could help the readers to provide a better fix..

    – Kamran
    Nov 22 '18 at 22:07











  • You're right, I edited the question

    – Matías Contreras Selman
    Nov 26 '18 at 12:46











  • Please check the updated answer.

    – Kamran
    Nov 26 '18 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 '18 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 '18 at 15:23














1












1








1








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
}









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 12:45







Matías Contreras Selman

















asked Nov 22 '18 at 21:21









Matías Contreras SelmanMatías Contreras Selman

175110




175110













  • You forget to include DateFormatProtocol declaration that could help the readers to provide a better fix..

    – Kamran
    Nov 22 '18 at 22:07











  • You're right, I edited the question

    – Matías Contreras Selman
    Nov 26 '18 at 12:46











  • Please check the updated answer.

    – Kamran
    Nov 26 '18 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 '18 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 '18 at 15:23



















  • You forget to include DateFormatProtocol declaration that could help the readers to provide a better fix..

    – Kamran
    Nov 22 '18 at 22:07











  • You're right, I edited the question

    – Matías Contreras Selman
    Nov 26 '18 at 12:46











  • Please check the updated answer.

    – Kamran
    Nov 26 '18 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 '18 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 '18 at 15:23

















You forget to include DateFormatProtocol declaration that could help the readers to provide a better fix..

– Kamran
Nov 22 '18 at 22:07





You forget to include DateFormatProtocol declaration that could help the readers to provide a better fix..

– Kamran
Nov 22 '18 at 22:07













You're right, I edited the question

– Matías Contreras Selman
Nov 26 '18 at 12:46





You're right, I edited the question

– Matías Contreras Selman
Nov 26 '18 at 12:46













Please check the updated answer.

– Kamran
Nov 26 '18 at 12:50





Please check the updated answer.

– Kamran
Nov 26 '18 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 '18 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 '18 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 '18 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 '18 at 15:23












1 Answer
1






active

oldest

votes


















1














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
}





share|improve this answer


























  • Man this really helped me. Thanks

    – Matías Contreras Selman
    Nov 26 '18 at 13:52












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
});


}
});














draft saved

draft discarded


















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









1














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
}





share|improve this answer


























  • Man this really helped me. Thanks

    – Matías Contreras Selman
    Nov 26 '18 at 13:52
















1














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
}





share|improve this answer


























  • Man this really helped me. Thanks

    – Matías Contreras Selman
    Nov 26 '18 at 13:52














1












1








1







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
}





share|improve this answer















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
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 12:56

























answered Nov 22 '18 at 22:13









KamranKamran

7,34321129




7,34321129













  • Man this really helped me. Thanks

    – Matías Contreras Selman
    Nov 26 '18 at 13:52



















  • Man this really helped me. Thanks

    – Matías Contreras Selman
    Nov 26 '18 at 13:52

















Man this really helped me. Thanks

– Matías Contreras Selman
Nov 26 '18 at 13:52





Man this really helped me. Thanks

– Matías Contreras Selman
Nov 26 '18 at 13:52




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?