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
Q

Create subclass to inherit from super class with new property with default value of 2.0

A
class SubClass: SuperClass {
var newProperty = 2.0
}
26
Q

closed range operator

A

x…y // Starts at x and ends at y

27
Q

half-open range operator

A

x..

28
Q

One-sided range operator

A

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
Q

The Ternary Operator

A

condition ? true expression : false expression

30
Q

Nil Coalescing Operator

A
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
Q

repeat … while loop

A
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
Q

“break” statement

A
var j = 10
for _ in 0 ..<100
{
j += j
if j > 100 {
break
}
print ("j = \ (j) ")
}
33
Q

“continue” statement

A
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
Q

if … else if …

A
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
Q

guard statement

A
guard  ese {
// code to be executed if expression is false
}
// code here is executed if expression is true
36
Q

switch statement

A
switch expression
{
case match1:
         statements
case match2:
         statements
case match3, match4:
         statements
default:
          statements
}
37
Q

Function with default function parameter

A
func buildMessageFor(_ name: String = "Customer, count: Int ) -> String
}
  return ( "\(name), you are customer number \ (count) " )
}
38
Q

Function that returns multiple result values

A
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
Q

closure expression

A
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
Q

closure expression configured to accept parameters and return results

A
{ (  : ,  , ... ) -> 
< return type> in
 // Closure expression code here
}
41
Q

An array may be initialized with a collection of values (referred to as an array literal) at creation time using the following syntax

A

var variableName: [type] = [value 1, value2, value3, ……….]

42
Q

The following syntax can be used to create an empty array

A

var variableName = [type] ( )

43
Q

Array initialized to a specific size with each array element pre-set with a specified default value

A

var nameArray = [String] (repeating: “My String”, count: 10)

44
Q

Syntax to create a new array by adding together two existing arrays

A

let thirdArray = firstArray + secondArray

45
Q

forEach( ) array method

A

arrName.forEach { element in print(element) }

46
Q

A new dictionary may be initialized with a collection of values (referred to as a dictionary literal) at creation time using the following syntax

A

var variableName: [key type: value type] = [key 1: value 1, key 2, value2 …. ]

47
Q

Syntax to create an empty dictionary

A

var variableName = key type: value type

48
Q

The following code creates an empty dictionary designated to store integer keys and string values

A

var myDictionary = [Int: String] ( )

49
Q

The following code iterates through all the entries in a dictionary, outputting both the key and value for each entry

A

for (keyName, valueName) in arrayName {
print (“Key Name: (keyName), valueName: (valueName)”)
}