Use of self in swift
The ambiguity that is generated when the method is invoked to eliminate access properties.
When a function's parameter name has the same name as its own property name, for example:
/* Use Self to indicate whether the access is either its own attribute or parameter
*
/class AClass {
var greeting:string
init (greeting:string) {
// Use self to differentiate properties and parameters
self.greeting = Greeting
}
}
When you call its own specified constructor in a convenient constructor, for example:
Convenience init () {/
*
must use self because, in accordance with the rules constructed in paragraph two,
self cannot be used until the first phase of initialization is complete,
and because of the attributes of the object-oriented language,
All initialization method names are init,
no self, and the system does not know who called Init
/self.init ()
//Initialization
}
When you access your own properties and call your own methods in a closure, for example:
Uiview.animatewithduration (0.25) {()-> Void in/
*
closures may be thrown, and they must know who the methods and attributes belong to,
so use self
/
self.layoutifneeded ()
}
In a value-type method modified by mutating, when the Self property is modified, for example:
struct Point {
var x = 0.0, y = 0.0
mutating func movebyx (deltax:double, y deltay:double) {
self = point x: x + deltax, y:y + deltay)
}
}
Thank you for reading, I hope to help you, thank you for your support for this site!