Monday, January 19, 2015

Functions

Declaration

func DoSomeWork(param1: String, param2: Int)
{
    println("I saw \(param1) \(param2) times.")
}
// How to call
DoSomeWork("Sally", 3)

// Default Parameters
func DoSomeWork(param1: String, param2: Int = 2)
{
    println("I saw \(param1) \(param2) times.")
}
// How to call
DoSomeWork("Sally")
// or
DoSomeWork("Sally", param2: 5)

// Note: With default parameters you HAVE to use the 
// parameter name.

//Multiple Default Parameters
func DoSomeWork(name: String, howMany: Int=1, didSee: Bool=false)
{
    if didSee {
        println("I saw \(name) \(howMany) times.")
    }
    else {
        println("I did not see \(name) at all.")
    }
}

// How to call
DoSomeWork("Sally")
// or
DoSomeWork("Sally", didSee: true)
// or
DoSomeWork("Mark", didSee: true, howMany: 3)

// Notice how I can reorder the default parameters.

External Parameter Names

// Add extra argument info (called "argument labels")
// to help developers better understand what is happening
func DoSomeWork(forThisPerson param1: String, thisManyTimes param2: Int)
{
    println("I worked for \(param1) \(param2) times.")
}
How to call
DoSomeWork(forThisPerson: "Sally", thisManyTimes: 3)

// Note: If you do not include the argument labels 
// (external parameter names) you will get this issue:
// "Missing argument label 'thisManyTimes:' in call"

// One Parameter Name
// If you just want one parameter name but you still want 
// to force developers to type it out when calling then 
// you can use the pound (#) symbol in front of the parameter name.
func DoSomeWork(#forThisPerson: String, #thisManyTimes: Int)
{
    println("I worked for \(forThisPerson) \(thisManyTimes) times.")
}
How to call

DoSomeWork(forThisPerson: "Sally", thisManyTimes: 3)
Return a variable
func ReturnVariable() -> Bool
{
    return true
}

Control Flow

If

if myFirstName == "Mark"{
    print("Hello, \(myFirstName). You are an admin.")
}
else {
    print("Welcome, guest!")
}

Ternary Conditional Operator

 
let isAdmin: Bool = myFirstName == "Mark" ? true : false
 

Switch

switch myFirstName {
    case "Mark":
    print("Hello, \(myFirstName). You are an admin.")
case "Mark", "Chase":
    print("Hello, \(myFirstName). You are a user.")
default:
    print("Welcome, guest!")
}

// Note: No "break" statement is necessary.

For In

let iterations = 5

for index in 1...iterations {
    print("This is iteration \(index)")
}

// Note: The for loop is kind of deprecated, replaced by the for in loop.
for var x = 0; x < iterations; ++x {
    print("This is iteration \(x)")
}
For more examples of for in loops see post on Collections

While

var x = 0

while x < 5 {
    print("This is iteration \(x)")
    x++
}

repeat {
    print("This is iteration \(x)")
    x++
} while x < 5
(Updated for Swift 2.2)

Member Declarations

Variable Members

var myFirstName = "Mark" // Implicit - Will make variable a string
var myLastName: String = "Moeykens" // Explicit
var accountBalance: Float = 5012.123456
var measure: Double = 1.123456789012345
var isBlogging = true // Implicitly converts "isBlogging" to boolean

Constant Members 

 
let thing = "tree"
thing = "stone" // This is invalid
 

SwiftUI Search & Filter with Combine - Part 3 (iOS, Xcode 13, SwiftUI, 2...

In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...