Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Swift have an implicit Object Initializer, like in C#?

Tags:

swift

In C#, we have Object Initializers, like so:

    Person obj = new Person
    {
        FirstName = "Craig",
        LastName = "Playstead",
    };

Does Swift have this?

As an example, I have this code:

var config = IndicatesConfig()
config.name = NSLocalizedString(Localizable.FOLDERS, comment: "").uppercaseString
config.style = .DetailHeader
return config

But would like to do something along the lines of:

var config = IndicatesConfig() {
    name = NSLocalizedString(Localizable.FOLDERS, comment: "").uppercaseString
    style = .DetailHeader
}

Thank you!

edit:

I'm not referencing to the explicit definition of class initialisers. Please bear in mind the syntax shown in the C# example.

like image 740
nmdias Avatar asked Nov 19 '14 14:11

nmdias


People also ask

How do you initialize an object 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. For example, class Wall { ... // create an initializer init() { // perform initialization ... } }

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.

What is an initializer in Swift?

In this tutorial, we will learn about the Swift initializer and its type with the help of examples. 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.

When can I use self in Swift?

After all stored properties have been initialized, one is free to use self in any manner. There are two kinds of initializers in Swift: designated initializers and convenience initializers.

Do Swift subclasses inherit their superclass initializers?

Unlike subclasses in Objective-C, Swift subclasses don’t inherit their superclass initializers by default. Swift’s approach prevents a situation in which a simple initializer from a superclass is inherited by a more specialized subclass and is used to create a new instance of the subclass that isn’t fully or correctly initialized.

How do you initialize a new class in Swift?

Class initialization in Swift is a two-phase process. In the first phase, each stored property is assigned an initial value by the class that introduced it. Once the initial state for every stored property has been determined, the second


2 Answers

Not as such. If you create a custom struct, Swift will, under certain conditions, create a default memberwise initializer that is close to what you're looking for. But otherwise, I don't think there's even a way to implement such a feature, since Swift lacks anything like a with keyword that would get you into the new instance's scope.

Update: this is as close as I can get, by defining a custom operator:

infix operator <| { }

func <|<T>(decl: @autoclosure () -> T, f: T -> () ) -> T {
    let obj = decl()
    f(obj)
    return obj
}

let label = UILabel() <| {
    $0.frame = CGRect(x: 10, y: 10, width: 300, height: 25)
    $0.text = "Hello"
    $0.enabled = false
}

println(label)
// <UILabel: 0x7fb46240b210; frame = (10 10; 300 25); text = 'Hello'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fb46240c2b0>>
like image 156
Nate Cook Avatar answered Sep 21 '22 12:09

Nate Cook


Swift doesn't have such feature.

You can implement similar interface using closure, but...

class MyClass {
    var name = ""
    var style = 0
    init(initializer:((MyClass)->Void)? = nil) {
        initializer?(self)
    }
}

let a = MyClass2() {
    $0.name = "foo"
    $0.style = 2
}

There is no implicit self here, instead, you have to use implicit closure parameter $0.

like image 27
rintaro Avatar answered Sep 25 '22 12:09

rintaro