Advance Swift: Generated class methods and how to use them.
To begin with we have to understand class methods. What are they?
Class Method
They work almost similar to static methods. Both static methods and class method belongs to the type itself rathen then the instance of type. Start a new Xcode’s playground and run the following code.
class Car {
class func whoAmI() {
print("I am a", self)
}
static func whatIsMyName() {
print("My name is ", self)
}
}
//MARK: - Class methods
Car.whoAmI() // I am a Car
Car.whatIsMyName() // I am a ModerenCar
Wait… There is a key difference too! Static methods can not be overridden but class methods can be overridden.
Following is the example of overridden method
class Honda: Car {
override class func whoAmI() {
print("I am a", self)
}
}
Honda.whoAmI() // I am a Honda
Did you noticed which method is invoked? This time method from the Subclass is invoked. That’s enough for the introduction of class methods.
Generated Class Method
Now, you must be thinking what “What was generated class method that you were going to explain”. Fortunately, I got you covered. Have a look at the following code.
Note: Find the explanation for the commented number after the code. We will continue this patteren throughout the article.
extension Int {
// 1
func toSpell() -> String? {
let formatter = NumberFormatter()
formatter.numberStyle = .spellOut
return formatter.string(from: self as NSNumber)
}
// 2
func cube() -> Int {
return self * self * self
}
}
1.toSpell() // one
2.toSpell() // two
2.cube() // 8
Yep, you are thinking correct. Nothing fancy here, just two simple methods.
- This method will spell out the integer number.
- This method will calculate and return cube of integer.
Let’s begin with the fun part. Swift automatically generates the class methods for the above added methods. Don’t you believe me? Let’s have a look at code completion in the Xcode’s Playground.
Take a look at above image, how the toSpell()
method is being autocompleted with Int
type itself. Wait a sec, did you notice at the signature of method? How will we use this method with the above signature?
How to use Generated Class Method
Int.toSpell(2)() // two
Why we need parenthesis after calling the method? Let’s break down by diving a bit in to the realm of closures. Can you guess what will be it’s signature, if this method will be assigned to a new property?
let toSpellSign = Int.toSpell // What is the type of toSpellSign?
It is (Int) -> () -> String?
What is this signature?
It’s a closure that will take (Int)
(integer) and will ->
(return) a() -> String?
(closure) and that closure will take ()
(nothing) and will ->
(return) String?
(optional string). Let’s verify this
let spellSignClosure = toSpellSign(2) // () -> String?
The above code take integer as argument and return a new closure () -> String?
which is returning string.
let spelledOutInteger = spellSignClosure() // two
In the above code, after running the closure it will return a string as it was promised.
Clap if liked this, that’s all…
Want to connect?
Click here for connecting on linkedIn.