1.

Explain the difference between Self and self in ios Swift.

Answer»

There is a distinction between Self (CAPITAL S) and self (small S) when WRITING protocols and protocol EXTENSIONS. When USED with a capital S, Self refers to the protocol COMPLIANT type, such as String or Int. When used with a lowercase S, self refers to the value contained within that type, such as "hi" or 999, for instance.

Consider the following SquareInteger extension as an example:

extension SquareInteger { func squareANumber() -> Self { return self * self }}

Remember that Self with a capital S refers to any type that follows the protocol. Because Int conforms to SquareInteger in the example above, when called on Int, the method returns an Int.  Self with a lowercase S, on the other hand, refers to the type's current value. If the preceding example were run on an Int containing the value 4, the result would be 4 * 4.



Discussion

No Comment Found