Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Block in Swift

Tags:

static

swift

In Java, I use static block to execute some code when the class is called like in this example"

Class Name
 {
     static
     {
         for(int i = 0; i<10; i++)
         {

         }
     } 
 }

How do I translate that code in Swift?

like image 998
kaneyip Avatar asked Oct 31 '22 19:10

kaneyip


1 Answers

You could do something like this,.

class SomeViewController : UIViewController {
    public static let formatter: DateFormatter = {
        let df = DateFormatter()
        df.dateFormat = "yyyy-MM-dd"
        return df
    }()
}
like image 171
Anand Rockzz Avatar answered Dec 22 '22 14:12

Anand Rockzz