Lynn Marks
Lynn Marks

Reputation: 93

Two equal signs in expression Swift

I've added a Date extension that a variable for a Gregorian calendar like this:

extension Date {
    struct Gregorian {
        static let calendar = Calendar(identifier: .gregorian)
    }
}

I want to add another static var inside the Gregorian struct for a calender where the firstWeekday = 2. Something like this:

struct Gregorian {
    static let calendar = Calendar(identifier: .gregorian)
    static let calender2 = Calendar(identifier: .gregorian).firstWeekday = 2
}

However, I can't have a statement with two =. How can I properly achieve adding this new struct member?

Upvotes: 2

Views: 58

Answers (1)

vacawama
vacawama

Reputation: 154603

Define and call a closure to create calendar2:

struct Gregorian {
    static let calendar = Calendar(identifier: .gregorian)
    static let calender2: Calendar = {
        var c = Calendar(identifier: .gregorian)
        c.firstWeekday = 2
        return c
    }()
}

Upvotes: 3

Related Questions