Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"initialize" class method for classes in Swift?

Tags:

ios

swift

I'm looking for behavior similar to Objective-C's +(void)initialize class method, in that the method is called once when the class is initialized, and never again thereafter.

A simple class init () {} in a class closure would be really sleek! And obviously when we get to use "class vars" instead of "static vars in a struct closure", this will all match really well!

like image 928
aleclarson Avatar asked Jun 10 '14 09:06

aleclarson


People also ask

What does init () do in Swift?

Swift init() Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready for use.

How do you instantiate a class in Swift?

Instantiating an instance of a class is very similar to invoking a function. To create an instance, the name of the class is followed by a pair of parentheses, and the return value is assigned to a constant or variable. In our example, the constant john now points to an instance of the Person class.

What is class initializer in Swift?

An initializer is a special type of function that is used to create an object of a class or struct. In Swift, we use the init() method to create an initializer.

How do you initialize a structure in Swift?

In Swift, all structs come with a default initializer. This is called the memberwise initializer. A memberwise initializer assigns each property in the structure to self. This means you do not need to write an implementation for an initializer in your structure.


2 Answers

If you have an Objective-C class, it's easiest to just override +initialize. However, make sure subclasses of your class also override +initialize or else your class's +initialize may get called more than once! If you want, you can use dispatch_once() (mentioned below) to safeguard against multiple calls.

class MyView : UIView {   override class func initialize () {     // Do stuff   } } 

 

If you have a Swift class, the best you can get is dispatch_once() inside the init() statement.

private var once = dispatch_once_t()  class MyObject {   init () {     dispatch_once(&once) {       // Do stuff     }   } } 

This solution differs from +initialize (which is called the first time an Objective-C class is messaged) and thus isn't a true answer to the question. But it works good enough, IMO.

like image 118
aleclarson Avatar answered Sep 19 '22 17:09

aleclarson


There is no type initializer in Swift.

“Unlike stored instance properties, you must always give stored type properties a default value. This is because the type itself does not have an initializer that can assign a value to a stored type property at initialization time.”

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.


You could use a type property which default value is a closure. So the code in the closure would be executed when the type property (or class variable) is set.

class FirstClass {     class var someProperty = {      // you can init the class member with anything you like or perform any code         return SomeType     }() } 

But class stored properties not yet supported (tested in Xcode 8).

One answer is to use static, it is the same as class final.

Good link for that is

Setting a Default Property Value with a Closure or Function

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.


Code example:

class FirstClass {     static let someProperty = {         () -> [Bool] in         var temporaryBoard = [Bool]()         var isBlack = false         for i in 1...8 {             for j in 1...8 {                 temporaryBoard.append(isBlack)                 isBlack = !isBlack             }             isBlack = !isBlack         }          print("setting default property value with a closure")         return temporaryBoard     }() }  print("start") FirstClass.someProperty 

Prints

start

setting default property value with a closure

So it is lazy evaluated.

like image 22
Binarian Avatar answered Sep 21 '22 17:09

Binarian