Swift Syntax Flashcards

1
Q

for-in loop

A
for  in  {
// code to be executed
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

if statement

A
var Christmas = true
if Christmas { print("A Savior has been born!") }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

function

A
func  (: ,
                                       : , ... ) ->  {
// Function code
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

calling a function with specified parameter/data type

A

funcName(paramName:paramArgument)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

while loop

A
while condition {
  // Execute while condition remains true
}
example
while total < 50 {
  let diceRoll = Int.random(in: 1...6)
  total += diceRoll
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

for-in loops

A

used to iterate over collection items like ranges and strings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

stride( )

A

creates a range that we can customize.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Create an empty dictionary using dictionary literal syntax

A

var dictionaryName: [KeyType: ValueType] = [:]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

create an empty dictionary using initializer syntax

A

var dictionaryName = KeyType: ValueType

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

create a populated dictionary

A
var dictionaryName: [KeyType: ValueType] = [
  Key1: Value1,
  Key2: Value2,
  Key3: Value3
]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

create a dictionary with type inference

A

var fruitStand = [
“Apples”: 12,
“Bananas”: 20
]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

add new key-value to dictionary (or update existing)

A

dictionaryName[NewKey] = NewValue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

update dictionary value using updateValue() method

A

dictionaryName.updateValue(“NewValueName”, forKey: “KeyName”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

remove key-value pair from dictionary using nil

A

dictionaryName[“keyName”] = nil

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

remove key-value pair from dictionary using .removeValue( ) method

A

dictionaryName.removeValue(forKey:”keyName”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

remove all elements from a dictionary

A

dictionaryName.removeAll( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

determine if a dictionary is empty

A

dictionaryName.isEmpty

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

determine the number of elements contained in a dictionary

A

dictionaryName.count

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

extract a value from dictionary and assign it to a new variable

A

var newVariable = dictionaryName[“keyName”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

if-let statement

A
if let newVarName = dictionaryName["keyName"] {
action to perform
}
21
Q

iterate through a dictionary

A
for (keyHolder, valueHolder) in dictionaryName {
  // Body of loop
}
22
Q

The syntax for creating an instance

A

var ferris = Student()

23
Q

Syntax for init() method

A
class className {
  var property = value
  init (property: valueType) {
     self.property = property
  }
}
24
Q

Syntax to create a subclass that inherits from a superclass

A
class Subclass: Superclass {
}
25
Create subclass to inherit from super class with new property with default value of 2.0
``` class SubClass: SuperClass { var newProperty = 2.0 } ```
26
closed range operator
x...y // Starts at x and ends at y
27
half-open range operator
x..
28
One-sided range operator
x... /* or */ ...y // specifies a range that can extend as far as possible in a specified range direction until the natural beginning or end of the range is reached, can be used w/strings.
29
The Ternary Operator
condition ? true expression : false expression
30
Nil Coalescing Operator
``` let customerName: String? = nil print("Welcome back, \(customerName ?? "customer")") //The above example will output text which reads "Welcome back, customer" because the customerName optional is set to nil. ```
31
repeat ... while loop
``` repeat { // Swift statements here } while EX. var i = 10 repeat { i -= 1 } while (i > 0) //In the repeat .. while example above the loop will continue until the value of a variable named i equals 0 ```
32
"break" statement
``` var j = 10 for _ in 0 ..<100 { j += j if j > 100 { break } print ("j = \ (j) ") } ```
33
"continue" statement
``` var i = 1 while i < 20 { i += 1 if ( i % 2 ) != 0 { continue } print ( " i = \ ( i ) " ) } // The "continue" statement in the above example will cause the print call to be skipped unless the value of "i" is even. // I.E. The print call will be skipped unless the condition is met ```
34
if ... else if ...
``` if let x = 9 if x == 10 { print ( " x is 10 " ) } else if x == 9 { print ( " x is 9 " ) } else if x == 8 { print ( " x is 8 " ) } ```
35
guard statement
``` guard ese { // code to be executed if expression is false ``` ``` } // code here is executed if expression is true ```
36
switch statement
``` switch expression { case match1: statements case match2: statements case match3, match4: statements default: statements } ```
37
Function with default function parameter
``` func buildMessageFor(_ name: String = "Customer, count: Int ) -> String } return ( "\(name), you are customer number \ (count) " ) } ```
38
Function that returns multiple result values
``` func sizeConverter (_ length: Float ) -> (yards: Float, centimeters: Float, meters: Float) { let yards = length * 0.0277778 let centimeters = length * 2.54 let meters = length * 0.0254 ``` return (yards, centimeters, meters) }
39
closure expression
``` The following code declares a closure expression and assigns it to a constant named sayHello and then calls the function via the constant reference EX: let sayHello = { print( "Hello" ) } sayHello( ) ```
40
closure expression configured to accept parameters and return results
``` { ( : , , ... ) -> < return type> in // Closure expression code here } ```
41
An array may be initialized with a collection of values (referred to as an array literal) at creation time using the following syntax
var variableName: [type] = [value 1, value2, value3, ..........]
42
The following syntax can be used to create an empty array
var variableName = [type] ( )
43
Array initialized to a specific size with each array element pre-set with a specified default value
var nameArray = [String] (repeating: "My String", count: 10)
44
Syntax to create a new array by adding together two existing arrays
let thirdArray = firstArray + secondArray
45
forEach( ) array method
arrName.forEach { element in print(element) }
46
A new dictionary may be initialized with a collection of values (referred to as a dictionary literal) at creation time using the following syntax
var variableName: [key type: value type] = [key 1: value 1, key 2, value2 .... ]
47
Syntax to create an empty dictionary
var variableName = [key type: value type]( )
48
The following code creates an empty dictionary designated to store integer keys and string values
var myDictionary = [Int: String] ( )
49
The following code iterates through all the entries in a dictionary, outputting both the key and value for each entry
for (keyName, valueName) in arrayName { print ("Key Name: \(keyName), valueName: \(valueName)") }