What is different between Self and self in Swift?

I'm newbie in Swift. When I learn DemoBots sample code, I don't understand What is different between Self and self in Swift?


DemoBots

Swift/BaseScene.swift

static func sceneWithFileName(fileName: String) -> Self {
     let scene = self(fileNamed: fileName)!

Accepted Reply

"self" refers to the current instance, in the body of one of its methods.


"Self" is a placeholder used in two different cases:


1. In a protocol, it refers to the type that conforms to the protocol in any particular use. In Equatable, for example, it's used to require that the two values being compared are of the same type. It's something like a generic type parameter that you don't have to put between the <…> because it's deduced from the context of its use.


2. In a class/static method, it can be used as the return type, to indicate that the return type is the type of the class to which the method was sent, rather than the class in which the method is declared. It's similar to 'instancetype' in Obj-C.

Replies

"self" refers to the current instance, in the body of one of its methods.


"Self" is a placeholder used in two different cases:


1. In a protocol, it refers to the type that conforms to the protocol in any particular use. In Equatable, for example, it's used to require that the two values being compared are of the same type. It's something like a generic type parameter that you don't have to put between the <…> because it's deduced from the context of its use.


2. In a class/static method, it can be used as the return type, to indicate that the return type is the type of the class to which the method was sent, rather than the class in which the method is declared. It's similar to 'instancetype' in Obj-C.

Do you happen to know where the use of "Self" is documented (other than in compiler errors)?


I was going to write up a bug regarding the use of Self? as a return value for a class method, but wanted to check the details in the documentation first. Unfortunately, with case-insensitive search, trying to find Self instead of self is a bit tough.

No, it came from various sources that I don't remember, including several threads in the dev forums. A bug report on this sounds like an excellent idea.

Seen this SO thread?


http://stackoverflow.com/questions/27863810/swift-difference-between-self-and-self


Which links to: Protocol Associated Type Declaration


(wow...no moderation for a SO link)

◅▻

Post not yet marked as solved Up vote reply of KMT Down vote reply of KMT

"Protocol-Oriented Programming in Swift" in this session video, I get more "Self" explaination. Thanks QuinceyMorris.


https://developer.apple.com/videos/wwdc/2015/?id=408