for-in loop
for in {
// code to be executed
}if statement
var Christmas = true
if Christmas { print("A Savior has been born!") }function
func (: ,
: , ... ) -> {
// Function code
}calling a function with specified parameter/data type
funcName(paramName:paramArgument)
while loop
while condition {
// Execute while condition remains true
}
example
while total < 50 {
let diceRoll = Int.random(in: 1...6)
total += diceRoll
}for-in loops
used to iterate over collection items like ranges and strings.
stride( )
creates a range that we can customize.
Create an empty dictionary using dictionary literal syntax
var dictionaryName: [KeyType: ValueType] = [:]
create a populated dictionary
var dictionaryName: [KeyType: ValueType] = [ Key1: Value1, Key2: Value2, Key3: Value3 ]
create a dictionary with type inference
var fruitStand = [
“Apples”: 12,
“Bananas”: 20
]
add new key-value to dictionary (or update existing)
dictionaryName[NewKey] = NewValue
update dictionary value using updateValue() method
dictionaryName.updateValue(“NewValueName”, forKey: “KeyName”)
remove key-value pair from dictionary using nil
dictionaryName[“keyName”] = nil
remove key-value pair from dictionary using .removeValue( ) method
dictionaryName.removeValue(forKey:”keyName”)
remove all elements from a dictionary
dictionaryName.removeAll( )
determine if a dictionary is empty
dictionaryName.isEmpty
determine the number of elements contained in a dictionary
dictionaryName.count
extract a value from dictionary and assign it to a new variable
var newVariable = dictionaryName[“keyName”]
if-let statement
if let newVarName = dictionaryName["keyName"] {
action to perform
}iterate through a dictionary
for (keyHolder, valueHolder) in dictionaryName {
// Body of loop
}The syntax for creating an instance
var ferris = Student()
Syntax for init() method
class className {
var property = value
init (property: valueType) {
self.property = property
}
}Syntax to create a subclass that inherits from a superclass
class Subclass: Superclass {
}