Initialization of structures with a constructor in Swift
let size = Size(width: 10, height: 10)
let point = Point(top: 5, left: 5)
//without external parameters names
let rect = Rectangle(size, point)
struct Size {
var width, height: Int
init (width: Int, height: Int) {
self.width = width
self.height = height
}
}
struct Point {
var top, left: Int
init (top: Int, left: Int) {
self.top = top
self.left = left
}
}
struct Rectangle {
var size: Size
var point: Point
//without external parameters names
init (_ size: Size, _ point: Point) {
self.size = size
self.point = point
}
}
- Log in to post comments