Can I have a static ISO8601DateFormatter with specific formatOptions in Swift?

21 views Asked by At

I have a struct that needs to do consistent date formatting, so I want my struct to have a ISO8601DateFormatter with .formatOptions = .withFullDate.

If I have a constant on the struct instance, it's easy enough to set .formatOptions = .withFullDate in init().

struct MyStruct1 {
    let dateFormatter = ISO8601DateFormatter()
    init() {
        dateFormatter.formatOptions = .withFullDate
    }
    // Other functions go here
}

But if I have a static constant, then it's wrong to set .formatOptions = .withFullDate in init(), because it won't be set if I use the constant in a static function.

I tried this:

struct MyStruct2 {
    static let dateFormatter =
    ISO8601DateFormatter(
        formatOptions: .withFullDate
    )
    // Other functions go here
}

But that does not compile. I get Argument passed to call that takes no arguments.

Am I missing something? Is there a way to get a static ISO8601DateFormatter with formatOptions=.withFullDate?

0

There are 0 answers