What is the basic syntax to declare an actor in Swift?
Use the keyword actor followed by the actor's name and curly braces. For example:
actor MyActor {
// actor code here
}
Click to reveal answer
beginner
Can an actor in Swift have properties and methods?
Yes, actors can have properties and methods just like classes. These help manage state safely in concurrent code.
Click to reveal answer
intermediate
How do you mark a method inside an actor as asynchronous?
You add the async keyword after the method's parameter list and before the return type. For example:
func fetchData() async -> String {
// code
}
Click to reveal answer
intermediate
Why use actors instead of classes for concurrency in Swift?
Actors protect their internal state by serializing access, preventing data races in concurrent environments.
Click to reveal answer
beginner
How do you create an instance of an actor?
You create it like a class instance using the initializer:<br><pre>let myActor = MyActor()</pre>
Click to reveal answer
Which keyword is used to declare an actor in Swift?
Aactor
Bclass
Cstruct
Denum
✗ Incorrect
The actor keyword declares an actor type in Swift.
What is the main benefit of using actors in Swift?
AThey serialize access to their internal state to prevent data races.
BThey allow multiple threads to access state simultaneously.
CThey replace functions with asynchronous calls.
DThey automatically optimize memory usage.
✗ Incorrect
Actors serialize access to their internal state, preventing data races in concurrent code.
How do you mark a method inside an actor as asynchronous?
ANo special keyword is needed.
BAdd the <code>async</code> keyword before the return type.
CUse the <code>async</code> keyword after the method name.
DAdd the <code>await</code> keyword before the method name.
✗ Incorrect
Methods that perform asynchronous work inside actors are marked with async before the return type.
How do you create an instance of an actor named DataManager?
Anew DataManager()
Bactor dm = DataManager()
Clet dm = actor DataManager()
Dlet dm = DataManager()
✗ Incorrect
Actors are instantiated like classes using the initializer syntax.
Can actors in Swift have stored properties?
ANo, actors cannot have any properties.
BNo, actors can only have computed properties.
CYes, actors can have stored properties.
DOnly static properties are allowed.
✗ Incorrect
Actors can have stored properties to hold their internal state.
Explain how to declare an actor in Swift and why you would use one.
Think about how actors help with safe access to data when many things happen at once.
You got /4 concepts.
Describe how asynchronous methods work inside an actor and how you declare them.
Remember that async methods let you wait for work without blocking the whole program.
You got /4 concepts.
Practice
(1/5)
1. What keyword is used to declare an actor in Swift?
easy
A. class
B. enum
C. struct
D. actor
Solution
Step 1: Understand actor declaration
Actors in Swift are declared using a special keyword to indicate their concurrency-safe nature.
Step 2: Identify the correct keyword
The keyword actor is used to declare actors, differentiating them from classes, structs, or enums.
Final Answer:
actor -> Option D
Quick Check:
Actor declaration keyword = actor [OK]
Hint: Actors always start with the keyword 'actor' [OK]
Common Mistakes:
Using class instead of actor
Confusing struct with actor
Trying to declare actor with enum
2. Which of the following is the correct syntax to declare an actor named DataManager?
easy
A. actor DataManager() { }
B. actor DataManager { }
C. struct DataManager { }
D. class DataManager { }
Solution
Step 1: Recall actor declaration syntax
Actors are declared with the keyword actor followed by the name and curly braces without parentheses.
Step 2: Check each option
actor DataManager { } uses correct syntax: actor DataManager { }. actor DataManager() { } incorrectly adds parentheses after the name.
Final Answer:
actor DataManager { } -> Option B
Quick Check:
Correct actor syntax = actor Name { } [OK]
Hint: Actors do not use parentheses after their name [OK]
Common Mistakes:
Adding parentheses after actor name
Using class or struct instead of actor
Omitting curly braces
3. What will be the output of the following Swift code?
actor Counter {
var value = 0
func increment() {
value += 1
}
}
let counter = Counter()
Task {
await counter.increment()
print(await counter.value)
}
medium
A. 1
B. 0
C. Compilation error
D. Runtime error
Solution
Step 1: Understand actor behavior and method calls
The actor Counter has a variable value initialized to 0 and a method increment() that adds 1 to value. The method is called with await inside a Task.
Step 2: Trace the code execution
The increment() method increases value from 0 to 1. Then print(await counter.value) prints the updated value, which is 1.
Final Answer:
1 -> Option A
Quick Check:
Incremented value = 1 [OK]
Hint: Remember to await actor method calls and property access [OK]
Common Mistakes:
Forgetting to use await with actor calls
Expecting initial value 0 after increment
Assuming synchronous access to actor properties
4. Identify the error in the following actor declaration and fix it:
actor Logger {
var logs: [String]
func addLog(_ message: String) {
logs.append(message)
}
}
medium
A. Missing initialization of 'logs' array
B. Actor cannot have functions
C. Use 'class' instead of 'actor'
D. Missing 'await' keyword in function
Solution
Step 1: Check property initialization in actor
The property logs is declared but not initialized. In Swift, stored properties must have initial values or be initialized in an initializer.
Step 2: Identify the fix
Initialize logs as an empty array: var logs: [String] = [] to fix the error.
Final Answer:
Missing initialization of 'logs' array -> Option A
Quick Check:
Stored properties need initial values [OK]
Hint: Always initialize stored properties in actors [OK]
Common Mistakes:
Assuming default initialization for arrays
Thinking actors cannot have functions
Forgetting to add await when calling actor methods
5. You want to create an actor BankAccount that safely manages a balance. Which of the following code snippets correctly declares the actor and provides a method to deposit money asynchronously?
hard
A. actor BankAccount {
var balance: Double = 0.0
func deposit(amount: Double) {
balance += amount
}
}
B. actor BankAccount {
var balance: Double = 0.0
func deposit(amount: Double) {
await balance += amount
}
}
C. actor BankAccount {
var balance: Double = 0.0
func deposit(amount: Double) async {
await Task.sleep(1_000_000_000)
balance += amount
}
}
D. class BankAccount {
var balance: Double = 0.0
func deposit(amount: Double) async {
balance += amount
}
}
Solution
Step 1: Understand actor method concurrency
Actor methods that perform asynchronous work should be marked async. actor BankAccount {
var balance: Double = 0.0
func deposit(amount: Double) {
balance += amount
}
} lacks async on the method, so it cannot await inside.
Step 2: Check for realistic async behavior
actor BankAccount {
var balance: Double = 0.0
func deposit(amount: Double) async {
await Task.sleep(1_000_000_000)
balance += amount
}
} marks deposit as async and simulates asynchronous delay with await Task.sleep(), showing proper async usage inside an actor.
Step 3: Verify actor declaration
class BankAccount {
var balance: Double = 0.0
func deposit(amount: Double) async {
balance += amount
}
} uses class instead of actor, so it does not provide concurrency safety.
Final Answer:
correctly declares an actor with an async deposit method including await -> Option C
Quick Check:
Actor + async method + await inside = actor BankAccount {
var balance: Double = 0.0
func deposit(amount: Double) async {
await Task.sleep(1_000_000_000)
balance += amount
}
} [OK]
Hint: Async actor methods must be marked async and use await inside [OK]
Common Mistakes:
Omitting async keyword on async actor methods
Using class instead of actor for concurrency
Not using await inside async methods when needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.