Structs and Classes
Struct Methods
Methods are functions attached to a type and called on an instance.
Call behavior on a value
struct_methods.swift
struct Rectangle {
let width: Int
let height: Int
func area() -> Int {
return width * height
}
}
let width =
let rectangle = Rectangle(width: width, height: 4)
let area = rectangle.area()
print("width=\(width)")
print("area=\(area)")
struct Rectangle {
let width: Int
let height: Int
func area() -> Int {
return width * height
}
}
let width =
let rectangle = Rectangle(width: width, height: 4)
let area = rectangle.area()
print("width=\(width)")
print("area=\(area)")
struct Rectangle {
let width: Int
let height: Int
func area() -> Int {
return width * height
}
}
let width =
let rectangle = Rectangle(width: width, height: 4)
let area = rectangle.area()
print("width=\(width)")
print("area=\(area)")
methods
A method can read the instance's properties and return a calculated result.