class level or struct level method in swift like static method in Java?

To add a type-level method in a class, add the class keyword before the func declaration:

class Dealer {
    func deal() -> Card { ... }
    class func sharedDealer() -> Dealer { ... }
}

To add a type-level method in a struct or enum, add the static keyword before the func declaration:

struct Card {
    // ...
    static func fullDeck() -> Card[] { ... }
}

Both are generally equivalent to static methods in Java or class methods (declared with a +) in Objective-C, but the keyword changes based on whether you’re in a class or struct or enum. See Type Methods in The Swift Programming Language book.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)