Structures methods in Swift
Forums:
struct Point {
var x, y: Int
func toString() -> String {
return "x = \(x); y = \(y)"
}
mutating func move(right right: Int, down: Int) {
x += right
y += down
}
}
class func example() {
var p1 = Point(x: 1, y: 2)
let str1 = p1.toString()
//str1 is "x = 1; y = 2"
p1.move(right: 5, down: -1)
let str2 = p1.toString()
//str2 is "x = 6; y = 1"
- Log in to post comments