Although Metal can be written in Objective-C and C++, many Metal documents and tutorials use Swift. Therefore, this note provides a very quick review of the Swift language first.
Variable Declaration
Value Type Declaration
In Swift, variables can be declared with the following syntax.
Plain Text// Variables
var str = "Hello World"
var myVariable = 123
var myInt : Int = 5 // Explicit type declaration
// Constants
let constantVariable = 123
let myExplicitDouble : Double = 5.0
// Arrays
let intArray = [1,2,3,4,5]
let expIntArray : [Int] = [1,2,3,4,5]
// Tuples
let fileNotFound = (404, "File Not Found")
let serverError = (code: 500, message: "Internal Server Error")
// Note: tuples can provide an index for each value, so we can access the second item via serverError.message
// Dictionaries
var crew = ["A": "Tony", "B":"Aaron", "C":"Steve"]
print(crew["A"]) // "Tony"
crew["D"] = "Eric" // Add ("D", "Eric") to the dictionary
crew.removeValue(forKey:"D") // Remove ("D", "Eric")
The main difference between the keywords var and let is mutability. Variables declared with let are immutable; if you try to modify their value later, the compiler will report an error.
In the Swift standard library, all collection types such as Array, Dictionary, and Set are designed as value types. This means when you assign them to a variable or constant, or pass them as function parameters, you are actually working with copies. However, Swift uses Copy-on-Write (COW) for optimization, so the actual copy only happens when you modify the data after copying.
Functions
Declaration
Functions can be declared with the following syntax.
Plain Text// Procedure with no parameters
func HelloWorld() {
print("Hello World")
}
// Function with no parameters but a return value
func MyNumber() -> Int {
return 123
}
// Function with parameters
func Plus(firstVal: Int, secondVal: Int) -> Int {
return firstVal + secondVal
}
// Function with unlabeled parameters
func NoLabelPlus(_ num1 : Int, _ num2 : Int) -> Int {
return num1 + num2
}
// Function with default parameter values
func Increment(num1: Int, num2 : Int = 1) -> Int {
return num1 + num2
}
// Function with variadic parameters of the same type
func SumNumbers(numbers : Int...) -> Int {
var total = 0
for num in numbers {
total += number
}
return total
}
// Pass by reference using inout keyword
func Swap(firstVal : inout Int, secondVal : inout Int) {
(firstVal, secondVal) = (secondVal, firstVal)
}
Usage
Using the functions above as examples, here are some usage notes.
Plain Text// By default, you need to write all parameter labels (their names in the function)
// The first one can be omitted
Plus(firstVal: 1, secondVal:2) // 3
Plus(1, secondVal: 3) // 4
// For functions with unlabeled parameters, you don't need to write labels
NoLabelPlus(1,2) // 3
// For functions with default values, omit the parameter to use the default
Increment(1) // 2
// For variadic parameters, you can write them directly after the colon
SumNumbers(numbers: 1,2,3,4) // 10
// When passing by reference with inout, use the & operator
var a = 1
var b = 2
Swap(firstVal : &a, secondVal : &b)
a // 2
b // 1
Object-Oriented Programming
Classes
Objects
Reference:
- Learning Swift, 3rd Edition, Jonathon Manning
