Generic classes in Swift
Forums:
class Size<T> {
var width: T
var height: T
init(_ width: T, _ height: T) {
self.width = width
self.height = height
}
func asText() -> String {
return "[\(width); \(height)]"
}
}
let sizeInt = Size<Int>(5, 8)
let textInt = sizeInt.asText()
//textInt is "[5; 8]"
let sizeFloat = Size<Float>(3.7, 1.58)
let textFloat = sizeFloat.asText()
//textFloat is "[3.7; 1.58]"
- Log in to post comments