What's the difference between Self vs self ?
What's the difference between Self vs self ?
In Swift, the terms Self and self have distinct meanings and uses, which are crucial to understand for effective programming in the language.
selfself refers to the current instance of a class, struct, or enum.class Person {
var name: String
init(name: String) {
self.name = name // 'self' is used to distinguish the instance property from the initializer parameter
}
}
SelfSelf refers to the type of the current instance. It is often used in protocols and class hierarchies to ensure type integrity.Self is used to refer to the type that conforms to the protocol, allowing for polymorphic behavior.protocol Duplicatable {
func duplicate() -> Self
}
class Document: ...
middle