Extension
import Foundation
extension SomeType: SomeProtocol, AnotherProtocol {
}
extension Int {
mutating func square() {
self = self * self
}
var km: Int { return self * 1_000}
}
var ten = 10
print(ten.km)
ten.square()
print(ten)
struct Rect {
var width: Int
var height: Int
}
let square: Rect = Rect(width: 10, height: 10)
extension Rect {
init(center: Int, size: Int) {
}
}
extension Int {
enum Kind {
case negative, zero, positive
}
var kind: Kind {
switch self {
case 0:
return .zero
case let x where x > 0:
return .positive
default:
return .negative
}
}
}
extension Int {
subscript(index: Int) -> Int {
var decimal = 1
for _ in 0..<index {
decimal *= 10
}
return (self / decimal) % 10
}
}
print(746381295[0])