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.
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 ... } }
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.
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.
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.
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.
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
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>>
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With