Fundamentals Flashcards
Keyword to create an enumeration Type?
enum
What type of syntax do you use to access enums?
dot syntax
Can you use an enum in a switch statement?
Yes
How do you declare an enum with raw values?
enum ENUM_NAME: String {
case ENUM_CASE1 = “VALUE1”
case ENUM_CASE2 = “VALUE2”
}
How do you declare an enum with associated values?
enum ENUM_NAME { case ENUM_CASE1(CASETYPE1) case ENUM_CASE2(CASETYPE2) }
How do you access the value associated with an enum case in a switch statement?
case .ENUM_CASE(let VALUENAME):
What do you call variables within a Struct, Class?
Properties of a Struct, Class
Keyword to create different enumeration choices?
case
Do you have to write initializers for Structs?
No. Swift will automatically create a “memberwise initializer”
Do all properties of a struct need to be declared when instantiating an instance of a structure?
Yes
What do you call the object created from a Struct/Class?
Instance of a Struct/Class
Are dictionaries ordered or unordered?
Unordered
How do you access the enum’s raw value?
.rawValue getter
When you declare a variable as an enum type and you are setting it, can you use a shorter syntax?
Yes, use dot syntax. (exp: .caseItem)
How do you declare and initialize a dictionary using a literal format?
var DICT = [
“KEY1”: “VALUE1”,
“KEY2”: “VALUE2”
]
What do you call functions within a Struct, Class?
Methods of a Struct, Class
Are “switch case” statements labels capitalized?
No
How do you force unwrap optionals?
Using the bang (!) symbol after a variable name
When you set the value of a dictionary key to nil, does it remove it from the collection?
Yes
How do you declare and initialize an empty dictionary of type String: Int
var DICT: [String: Int] = [:]
What method can you call that returns a tuple containing an index and the value?
array.enumerated()
When Iterating a dictionary, could you get a different order of items?
Yes
When getting the value of a dictionary does it return an optional value?
Yes
What keyword do you use to define a tuple?
Parenthesis (ITEM1, ITEM2)
When a function returns a tuple can you define the labels associated with it?
Yes func MYFUNC() -> (LABEL1: String, LABEL2: Int) { }
When creating an enum is it a Type?
Yes
Can you nest Enums within a Struct?
Yes. When accessing from outside of Struct, full path is required Exp. Struct.Enum.case
Can you use a function as a closure?
Yes, but it’s a code smell if you are not using it somewhere else.
Can you use index notation to access a specific tuple?
Yes
When you don’t label your closure function variables, are they still accessible?
Yes. $0, $1, $2 …
How do you decompose a tuple? into variables
let (FIRST, SECOND) = (1, 2)
I want to sort an array in place what method can I use?
array.sort(by:) { }
What’s the syntax to label closure parameters?
after curly braces wrap parameter names in parenthesis followed by the “in” keyword.
list.map { (item) in
}
What String method can you use to return a Bool if string ends with certain characters?
”“.hasSuffix(“ending”)
When is it appropriate to omit the return value when writing a closure
If the calling method’s closure parameter is the final or only parameter
How do you map over an array
array.map(transform:) { (item) in … }
Can you break up tuples using nested arrays into variables?
Yes.. Exp:
let (key, (value1, value2) = $0
What affect does adding underscores when storing a whole number have?
None. Swift ignores them
What method can you use to iterate over all items in a collection?
list.forEach(body:) { (item) in … }
How do you know that a method takes a closure?
Look in the “Developer Documentation.” The function definition will show a Function Type as a Type for the specified parameter.
Exp:
sort(by: (Element, Element) -> Bool)
(Element, Element) -> Bool ; Is the function type for the closure.
I want to sort an array without modifying the calling array, How do I do that?
let newArray = array.sorted(by:) { }
How you create a multi-line strings?
Use triple quotes to start and end multi-line strings
var text = “””
Text
“””
Can you add Emoji’s in a string?
Yes
How do you count the characters in a string?
use .count getter
What String method can you use to uppercase the characters?
.uppercased()
What String method can you use to get a Bool if string starts with certain characters?
”“.hasPrefix(“beginning”)
When storing long numbers, is it possible to break them down using comma’s?
No, but you can break them up using underscores instead.
Does Swift support shorthand operators?
Yes.
count += 1 ; count -= 1
What Int method can you use to get a Bool if integer is multiple of something?
120.isMultiple(of: 3)
What Bool method can you use to flip a boolean to the opposite value?
.toggle()
exp: var isOpen = true
isOpen.toggle()
What is a “Half-open Range Operator?”
1..<10 - Range between 1 and 10 excluding the last number, so 1-9.
What does “Operator Overloading” mean?
Allows the same operators (+-*/, and so on) to do different things depending on the data type you use them with
What is a “Compound Assignment Operator?”
Syntactical sugar for var = var + 1
var += 1
What affect does using “Comparison operators” on two strings?
Compares if they are in alphabetical order
Do you need to explicitly add a break statement to each switch case?
No. Swift only matches one case, if you need you need to more on to the next use the keyword: “fallthrough”
Example of a condition statement?
if a > 1 { } else if a < 1 {} else {}
What does “Conditional Short-Circuiting” mean?
Conditional expressions are evaluated from left-right, the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression.
Does Swift yell at you if you don’t use the value returned from a for .. in loop? If so, how do you avoid this?
Yes. Use an underscore as the value name to ignore it.
When building a conditional with multiple expressions, should you use parenthesis?
Yes. Specially if expression includes an OR expression
What is the “Ternary Operator?”
let answer: String = true ? “True” : “False”
When creating a switch statement, do you need to have a default case?
Yes. all cases must be exhaustive!
What is a “Closed Range Operator?”
1…10 - Range between 1 and 10 including the last number, so 1-10
How do you write a while loop?
while count <= 20 {
count += 1
}
When you “break” out of a loop that is a nested loop, will it break out of all the loops?
No. You will need to add a label to the outer loop to break out of nested loops
outerLoop: for..in { for..in { break outerLoop } }
How do you know to use for .. in or while loops?
For … in loop = Finite sequences
While loop = Unknown amount of sequences
When should you use the repeat {} while loop?
When you want your condition to execute at least once before checking the condition.
Example of repeat {} while
let numbers = [1, 2, 3, 4, 5] var random: [Int]
repeat {
random = numbers.shuffled()
} while random == numbers
What Array method can you use to create, and shuffle a new copy of an array?
[].shuffled()
What character can you use to ignore the value returned?
Use underscore (_)
Can you use range operators as switch cases?
Yes
case 1…10:
print(“numbers 1 to 10”)
What keyword can you use to exit loops immediately?
The “break” keyword