Reputation: 1267
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?
Upvotes: 7
Views: 4401
Reputation: 6658
You could do something like this,.
class SomeViewController : UIViewController {
public static let formatter: DateFormatter = {
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd"
return df
}()
}
Upvotes: 4
Reputation: 1
I have been struggling with this same question for the last couple days. The solution that I found (although somewhat inelegant) is the use of main.swift
.
This isn't really the same thing as a static block on a class in Java, but it may help with your particular issue.
Upvotes: 0