class
keyword, the class
name and curly braces, inside which the properties and methods of the class are declared. class Point { var x = 0.0 // sets the default value of x to 0 var y = 0.0 // sets the default value of x to 0 } // this creates a new Point instance using the default initializer var point = Point() point.x = 100 // sets the x property to 100 point.y = 200 // sets the y property to 200
Initialization is the process of preparing an instance of a class, structure, or enumeration for further use. It includes the initialization of each property and the execution of other settings or initializations necessary before the first use.
')
The initialization process is implemented using initializers, which are special methods that are called when declaring new instances of a class. Unlike Objective-C, initializers do not return anything in Swift, since they ensure that new class instances are initialized correctly.
class Point { var x var y }
class Point { // Class 'Point' has no initializers ( <code>Point</code> ) var x // Type annotation missing in pattern ( ) var y // Type annotation missing in pattern ( ) }
class Point { var x: Double var y: Double }
init
keyword. An initializer may have several parameters, or there may not be any at all. class Point { var x: Float var y: Float init(x: Float, y: Float) { self.x = x self.y = y } } // Example usage var point = Point(x: 100, y: 200)
?
after the type as shown in the example. If you declare a property in this way, it will have a default value of nil
. class Point { var x: Float? var y: Float? } var point = Point() // both the x and y properties are now set to nil point.x = 100.0 point.y = 200.0
User
class. Each class object will have the following fields: first name ( firstName
), last name ( lastName
), brief information about the user ( bio
). The first and last name fields will have optional values, and the brief information field will have a default value (that is, this field will be optional). class User { var firstName: String? var lastName: String? var bio: String = "I ♡ Swift!" } var user = User() // user = { firstName: nil, lastName: nil, bio: "I ♡ Swift!"}
class User { var firstName: String var lastName: String var bio: String = "I ♡ Swift!" // no bio provided init(firstName: String, lastName: String) { self.firstName = firstName self.lastName = lastName } // bio provided init(firstName: String, lastName: String, bio: String) { self.firstName = firstName self.lastName = lastName self.bio = bio } } var me = User(firstName: "Andrei", lastName: "Puni") // me = { firstName: "Andrei", lastName: "Puni", bio: "I ♡ Swift!"} var silviu = User(firstName: "Silviu", lastName: "Pop", bio: "I f**ing ♡ Swift!!!") // silviu = { firstName: "Silviu", lastName: "Pop", bio: "I f**ing ♡ Swift!!!"}
bio
argument a default value. class User { var firstName: String var lastName: String var bio: String init(firstName: String, lastName: String, bio: String = "I ♡ Swift!") { self.firstName = firstName self.lastName = lastName self.bio = bio } } var me = User(firstName: "Andrei", lastName: "Puni") // me = { firstName: "Andrei", lastName: "Puni", bio: "I ♡ Swift!"} var silviu = User(firstName: "Silviu", lastName: "Pop", bio: "I f**ing ♡ Swift!!!") // silviu = { firstName: "Silviu", lastName: "Pop", bio: "I f**ing ♡ Swift!!!"}
Identity operators
Since a class is a reference type, a situation may arise where the same instance of a class is denoted by several variables or constants. (This is not possible for structures and enumerations, since both are meaningful types and are copied when assigned and passed to a function as parameters)
Sometimes it is useful to find out that two variables or constants refer to the same instance of a class. There are two operators for this in Swift:
Are identical (===
)
Not identical (!==
)
===
) var users: User[] = [ ... ] // User[] means Array of Users var host = /* some user */ for user in users { if user === host { // host logic here println("this is the host") } else { // guest logic here println("this is a guest") } }
==
) does not work when comparing class objects, so you should not be afraid of such errors.Source: https://habr.com/ru/post/231739/
All Articles