What are initializer in Swift?

devquora
devquora

Posted On: Feb 22, 2018

 

Swift initializer is a special type of method that can be called to create a new instance of a particular type.

Swift provides a default initializer for any structure or class that provides default values for all of its non-optional properties and does not provide the initializer itself.

Swift initializer Example

class User {
    var first_name: String
    var last_name: String
    
    init(first_name: String, last_name: String) {
        self.first_name = first_name
        self.last_name = last_name
    }
}
let p = Person(first_name:"Santosh", last_name:"Botre")

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Swift Interview Questions

    What is IOS Swift?

    iOS swift is an intuitive and stalwart language for the iOS. It is joyful to deal with the writing stuff in Swift. It is very advanced and its syntax is also concise and expressive too...

    Swift Interview Questions

    What is Dictionary in Swift?

    It enables you to store the key-value pairs and access the value by providing the key.It is similar to that of the hash..

    Swift Interview Questions

    Explain Enum in Swift?

    Basically, it is a type that contains a group of various related values under the same umbrella...