Eray Diler Eray Diler - 6 months ago 85
Swift Question

Distinction in Swift between uppercase "Self" and lowercase "self"

While playing in a Swift playground I noticed that

Self
, with capital "S", is available along with the lowercase
self
. Is there any difference between them? If so, what are usages for these two, especially for
Self
?

Answer

Self refers to the type of the current "thing" inside of a protocol (whatever is conforming to the protocol). For an example of its use, see Protocol func returning Self.

The only official docs I've found for Self is in Protocol Associated Type Declaration in The Swift Programming Language. It surprisingly is not documented in the sections on protocols or on nested types.

Comments