Swift Functions, Closures, and Data Structures: Key Concepts for Students

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/89

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 1:29 AM on 5/7/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

90 Terms

1
New cards
What is a function in Swift?
A named, reusable block of logic that can take parameters and return a value.
2
New cards
How do you call a Swift function with argument labels?
By using the parameter labels in the function call, such as add(a: 3, b: 4).
3
New cards
What is a closure in Swift?
An unnamed block of code that can be stored, passed, or returned like a value.
4
New cards
How are closures different from functions when called?
They do not use argument labels when called.
5
New cards
What is trailing closure syntax in Swift?
A syntax that allows the last closure parameter to be written outside the parentheses.
6
New cards
What is the full syntax for a Swift closure?
{ (params) -> ReturnType in code }
7
New cards
What is the simplified closure syntax when there are no parameters or return value?
{ code }
8
New cards
What is the type of the Swift function sayHello()?
() -> Void
9
New cards
Write a closure that takes two integers and returns their product.
let multiply = { (a: Int, b: Int) -> Int in return a * b }
10
New cards
What is wrong with this code: print(add(a: 3, b: 4))?
Argument labels cannot be used when calling it.
11
New cards
Why is _ used in a Swift closure parameter list?
It ignores an incoming value that is not needed.
12
New cards
How do you convert a closure parameter into trailing closure syntax?
Move the closure outside the parentheses when it is the final parameter.
13
New cards
What is a function in Swift?
A reusable block of code.
14
New cards
What is a method in Swift?
A function that belongs to a class, struct, or enum.
15
New cards
Can a struct contain methods in Swift?
Yes, it can contain functions inside it.
16
New cards
What are the two main uses of return in Swift?
To exit early or send back a value.
17
New cards
What happens when return is used before a print statement?
The print statement never runs.
18
New cards
What is wrong with this code: guard let name else { print("No name") }?
A control transfer statement such as return is missing.
19
New cards
What does randomElement() do in Swift?
It selects a random item from a collection and returns an optional.
20
New cards
Why is force unwrapping randomElement() risky?
It can crash if the collection is empty.
21
New cards
What does this code print: let result = names.randomElement() ?? "Z"?
A random element from the array.
22
New cards
What is optional unwrapping in Swift?
Retrieving the actual value from an optional.
23
New cards
What is forced unwrapping in Swift?
Using ! to directly access an optional value.
24
New cards
Why is optional binding safer than forced unwrapping?
It safely checks for nil before accessing the value.
25
New cards
What does the nil-coalescing operator ?? do?
It provides a default value if nil appears.
26
New cards
What does this code print: let value = Double(input ?? "10") ?? 0.0 when input is nil?
10.0
27
New cards
How do you safely convert a String? to Double using optional binding?
if let value = input, let d = Double(value) { print(d) }
28
New cards
What is a tuple in Swift?
A fixed group of related values.
29
New cards
Can tuples store different data types?
Yes, they can contain multiple types.
30
New cards
How do you access values in an unnamed tuple?
Using indexes like .0 or .1.
31
New cards
How do you access values in a named tuple?
Using property names like student.name.
32
New cards
What is tuple destructuring?
Splitting grouped values into separate variables.
33
New cards
What is the difference between sorted() and sort()?
One creates a new sorted array, while the other changes the original array.
34
New cards
Does sorted() modify the original array?
No, it leaves the original unchanged.
35
New cards
Does sort() create a new array?
No, it changes the existing array directly.
36
New cards
What does this code print: numbers.sorted { a, b in return a < b }?
The array in ascending order.
37
New cards
What is a UISlider in UIKit?
A UI control that lets the user choose a value by dragging.
38
New cards
What data type does UISlider.value return?
Float
39
New cards
What is a UIStepper in UIKit?
A UI control that increases or decreases a value using buttons.
40
New cards
What data type does UIStepper.value return?
Double
41
New cards
What is a struct in Swift?
A value type that can contain properties and methods.
42
New cards
What happens when you assign one struct variable to another?
A separate copy is created.
43
New cards
What is a class in Swift?
A reference type that supports shared objects.
44
New cards
What happens when you assign one class object to another variable?
Both variables point to the same object.
45
New cards
What is a computed property in Swift?
A property that calculates its value instead of storing it.
46
New cards
What is the default setter parameter name in Swift?
newValue
47
New cards
What is an instance method?
A function used with a specific object.
48
New cards
What is a type method in Swift?
A function that belongs to the type itself.
49
New cards
How do you define a type method in Swift?
Using static func or class func.
50
New cards
What does this code print: p2.name after changing p3.name when both reference the same class object?
The updated name value.
51
New cards
What happens when p.nextYearAge = 30 if the setter sets age = newValue - 1?
The age becomes 29.
52
New cards
When should you use an enum in Swift?
When working with a fixed set of related cases.
53
New cards
What does filter() do in Swift?
Creates a new array with elements that match a condition.
54
New cards
Does filter() modify the original array?
No, it returns a new one.
55
New cards
What does contains(where:) return?
A Boolean value.
56
New cards
Does contains(where:) return the matching element?
No, only true or false.
57
New cards
What does becomeFirstResponder() do?
Makes a UI control active, often showing the keyboard.
58
New cards
What does resignFirstResponder() do?
Makes a UI control inactive, often hiding the keyboard.
59
New cards
What is an extension in Swift?
A way to add new functionality to an existing type.
60
New cards
Can extensions add stored properties?
No, only computed properties can be added.
61
New cards
What is an instance property in an extension?
A property used with a specific value or object.
62
New cards
What is a type property in an extension?
A property that belongs to the type itself and uses static.
63
New cards
How do you access a type property in an extension?
By using the type name directly.
64
New cards
What is a protocol in Swift?
A blueprint that defines required properties and methods.
65
New cards
Can protocols contain stored properties?
No, only requirements can be declared.
66
New cards
What must a type do to adopt a protocol?
Implement all required properties and methods.
67
New cards
Can a class adopt multiple protocols?
Yes, multiple blueprints can be adopted.
68
New cards
Why are protocols useful in Swift?
They help create reusable and flexible code.
69
New cards
What is the role of UITableViewDataSource?
Providing row counts and cell content.
70
New cards
What is the role of UITableViewDelegate?
Handling user interaction and behavior.
71
New cards
What does tableView(_:numberOfRowsInSection:) do?
It tells how many rows should appear.
72
New cards
What does tableView(_:cellForRowAt:) do?
It creates and configures each visible cell.
73
New cards
Why is dequeueReusableCell used in UITableView?
To reuse cells and improve performance.
74
New cards
What does the is keyword do in Swift?
Checks whether a value matches a specific type.
75
New cards
What is upcasting in Swift?
Converting a subclass reference into a superclass reference.
76
New cards
What is downcasting in Swift?
Converting a superclass reference into a subclass reference.
77
New cards
What is the difference between as? and as!?
One safely returns an optional, while the other force casts and may crash.
78
New cards
What is Any in Swift?
A type that can store any value.
79
New cards
What is AnyObject in Swift?
A type that can store only class instances.
80
New cards
Can a struct be stored in AnyObject?
No, only class types are allowed.
81
New cards
What is optional chaining in Swift?
Safely accessing properties or methods through optionals.
82
New cards
What are the three common ways to transfer data in iOS?
Direct passing, delegate pattern, and notifications.
83
New cards
What is property passing in iOS?
Sending data directly through a variable.
84
New cards
Why is weak used for delegates?
To avoid memory leaks and strong reference cycles.
85
New cards
What does NotificationCenter.default.post do?
Broadcasts a message to listeners.
86
New cards
What does NotificationCenter.default.addObserver do?
Listens for broadcasted messages.
87
New cards
What is a dictionary in Swift?
A collection of unordered key-value pairs.
88
New cards
Why does dictionary access return an optional?
The requested key may not exist.
89
New cards
What happens when you set a dictionary key to nil?
The key-value pair is removed.
90
New cards
What does updateValue(_:forKey:) do in a dictionary?
It updates an existing value or adds a new one.