0
0
Swiftprogramming~20 mins

Nonisolated methods in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Actor Isolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a nonisolated method in an actor
What is the output of this Swift code using a nonisolated method inside an actor?
Swift
actor Counter {
    var value = 0
    nonisolated func description() -> String {
        "Counter value is not accessible here"
    }
    func increment() {
        value += 1
    }
}

let counter = Counter()
print(counter.description())
A0
BCounter value is not accessible here
CCompilation error: actor isolation violation
DRuntime error: data race detected
Attempts:
2 left
💡 Hint
Remember that nonisolated methods run outside the actor's isolation context.
Predict Output
intermediate
2:00remaining
Accessing actor state from nonisolated method
What happens if a nonisolated method tries to access an actor's isolated property directly?
Swift
actor Logger {
    var logs = [String]()
    nonisolated func latestLog() -> String {
        logs.last ?? "No logs"
    }
}

let logger = Logger()
print(logger.latestLog())
ACompilation error: actor-isolated property 'logs' cannot be accessed from nonisolated context
BPrints an empty string
CRuntime error: data race on 'logs'
DPrints the last log or "No logs" if empty
Attempts:
2 left
💡 Hint
Nonisolated methods cannot access actor-isolated properties directly.
🔧 Debug
advanced
2:00remaining
Why does this nonisolated method cause a compile error?
Identify the reason for the compile error in this code snippet.
Swift
actor BankAccount {
    var balance: Int = 1000
    nonisolated func getBalance() -> Int {
        balance
    }
}
AThe method getBalance() must be async to access actor properties.
BThe property 'balance' must be declared nonisolated.
CActors cannot have nonisolated methods.
DNonisolated methods cannot access actor-isolated properties like 'balance'.
Attempts:
2 left
💡 Hint
Think about actor isolation rules and what nonisolated means.
Predict Output
advanced
2:00remaining
Effect of nonisolated on method concurrency
What is the output of this Swift code involving a nonisolated method and an actor?
Swift
actor Printer {
    nonisolated func printMessage() {
        print("Hello from nonisolated method")
    }
    func printIsolatedMessage() {
        print("Hello from isolated method")
    }
}

let printer = Printer()
printer.printMessage()
Task {
    await printer.printIsolatedMessage()
}
AHello from nonisolated method\nHello from isolated method
BHello from isolated method\nHello from nonisolated method
CCompilation error: cannot call isolated method without await
DRuntime error: actor isolation violation
Attempts:
2 left
💡 Hint
Nonisolated methods can be called directly without await.
🧠 Conceptual
expert
2:00remaining
Why use nonisolated methods in actors?
Which of the following best explains the purpose of marking a method as nonisolated inside a Swift actor?
ATo allow the method to modify actor-isolated state without synchronization.
BTo make the method asynchronous and require awaiting before calling.
CTo allow the method to be called synchronously without actor isolation, typically for stateless or thread-safe operations.
DTo prevent the method from being called outside the actor's context.
Attempts:
2 left
💡 Hint
Think about when you want to avoid the overhead of actor isolation.