Extend an existing class

 Problem

I want to extend an existing class and add my own methods/functions to it.



 Recipe


// Extend the Int class
extension Int {

	// Add a method called 'multiply'
	func multiply(x:Int)->Int {
		return self*x
	}
}

// Now, all our Int's have the extra method
let y = 2.multiply(5)

print(y)
				


 Playground