“This is the 19th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”.
In Swift, self usually refers to the current object in a class or structure, and self represents any current type.
self
Self is the simplest and most common in Swift. It’s usually followed by one. And an attribute or function name. Such as
self.name = "Joke"
self.test()
Copy the code
This self is a reference to the current object (” instance “) by the class (or structure) in this class.
Explicit self
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .red
}
}
Copy the code
In the viewDidLoad() method, we use the self keyword. So we use self.view to refer to properties. You can eliminate any confusion about what you’re referring to by using an explicit self.
Implicit self
But in Swift, we’re used to not using self, so this is implicit. (Not writing does not mean that there is no 😁). Here’s a controller we often write:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
}
}
Copy the code
Why can I use self implicitly? That’s because a reference to the current object, we can use self, which is implicit. If we use the implicit self, Swift knows we are referring to the property of the current object, and it saves us coding time by making this assumption.
When to use explicit “self”?
- If you are working with a property and there is a naming conflict, use
self.
Explicitly tell Swift that you are referring to the property of the current object.
var num = 0
class ViewController: UIViewController {
var num = 0
override func viewDidLoad() {
super.viewDidLoad()
self.num = 0
}
}
Copy the code
or
class IccClass {
var name: String = "class prop"
func setProperty(name:String = "method attribute") -> () {
print(self.name) // Output: class property
print(name) // Output: method attribute
}
}
Copy the code
We need to note that the two names above represent different meanings. When you use self, you don’t confuse class attributes with local variables.
- Must be used when setting in closures
self.
To make capture semantics clear in closures
DispatchQueue.main.async {
self.someFunc(view: self.view)
}
Copy the code
A good rule of thumb is: you can always write implicit self, but you must use self when you need explicit or conflicting.
For example, resolving naming conflicts or making capture semantics clear in closures. You can add all the attributes self. To your code, but that doesn’t make it any clearer.
For more cases where you must implement explicit self, please let me learn 😁 in the comments section.
Self
In Swift, Self refers to a type — usually the current type in the current context. Just as lowercase self can represent the current object, uppercase self can represent the current type.
- In protocol, it refers to a type that conforms to protocol for any particular purpose.
Numeric {func squared() -> Self {return Self * Self}} 2.squared() 2.0.squared()Copy the code
In this context, Self refers to the type conforming to the Numeric protocol. In the example 2 to call Self will be the concrete type Int. If it is 2.0, Self will be the concrete type Double
If you want to restrict specific types, use Self in Extension with where we talked about earlier.
Numeric where Self == Int {func squared() -> Self {return Self * Self}} 2.squared() 2.0.squared() //Referencing instance method 'squared()' on 'Numeric' requires the types 'Double' and 'Int' be equivalentCopy the code
- In class/static methods, as a return type. Indicates that the return type is the type of the class in which the method is declared, not the class in which the method is declared. It’s similar to OC
instancetype
.
extension UILabel { func makeForTitle() -> Self { self.font = .boldSystemFont(ofSize: 24) self. TextColor =. DarkGray self. AdjustsFontSizeToFitWidth = true self. MinimumScaleFactor = 0.75 return self}} UILabel().makeForTitle()// can chain call....Copy the code