The arguments passed into the function can be simply passed in with an array dynamically
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
sumOf()
sumOf(42, 597, 12)
sumOf(222,3333,44444,555555)
Functions can be nested using
func returnFifteen ()-> Int {
var y = 10
// nested function
func add () {
y + = 5
}
// reference nested functions
add ()
return y
}
returnFifteen ()
A function can return another function as its value
func makeIncrementer ()-> (Int-> Int) {
func addOne (number: Int)-> Int {
return 1 + number
}
return addOne
}
// return addOne (number: Int)-> Int {....} to increment
var increment = makeIncrementer ()
// Incoming parameters
increment (7)
A function can use another function as its passed-in parameter
func hasAnyMatches(list: [Int], condition: Int -> Bool) -> Bool {
for item in list {
if condition(item) {
return true
}
}
return false
}
func lessThanTen(number: Int) -> Bool {
return number < 10
}
var numbers = [20, 19, 7, 12]
hasAnyMatches(numbers, lessThanTen)
You can write a closure without a name by surrounding code with braces ({}). Use with to separate the arguments and return type from the body.
var numbers = [20, 19, 7, 12]
// ---- multiply by 3 method one
numbers.map ({
(number: Int)-> Int in
let result = 3 * number
return result
})
// ---- multiply by 3 method 2
let mappedNumbers = numbers.map ({number in 3 * number})
mappedNumbers
// ---- sort
let sortedNumbers = sorted (numbers) {$ 0> $ 1}
sortedNumbers
Objects and classes
// class + class name
// {Property and method definition}
class Shape {
var numberOfSides = 4
let color = "# 3b5998"
func simpleDescription ()-> String {
return "A shape with \ (numberOfSides) sides."
}
func shadow (isShadowOn: Bool)-> String {
if (isShadowOn) {
return "This is ON"
} else {
return "This is OFF"
}
}
}
// first instantiate a class
// use class.propert class.method to refer to properties and methods
var shape = Shape ()
// rewrite property
shape.numberOfSides = 7
// call method
var shapeDescription = shape.simpleDescription ()
var shadow = shape.shadow (true)
// Define class Shape
class Shape {
// define attributes
let color = "# 000"
var numberOfSides = 0
var name: String
//initialization
init (name: String) {
self.name = name
}
// define method
func simpleDescription ()-> String {
return "A shape with \ (numberOfSides) sides."
}
}
// Create a new instance of Shape
var shape = Shape (name: "Test_Shape")
// Set properties
shape.numberOfSides = 7
// call method
var shapeDescription = shape.simpleDescription ()
// Subclass Square inherits from Shape
class Square: Shape {
// Add attributes
var sideLength: Double
// Custom initialization method
init (sideLength: Double, name: String) {
self.sideLength = sideLength
super.init (name: name)
numberOfSides = 4
}
// added method
func area ()-> Double {
return sideLength * sideLength
}
// Rewrite parent method
override func simpleDescription ()-> String {
return "A square with sides of length \ (sideLength)."
}
}
// Instantiate a Square
let test = Square (sideLength: 5.2, name: "my test square")
// call method
test.area ()
test.simpleDescription ()
Swift Basic 2