0
0
Swiftprogramming~10 mins

Nonisolated methods in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a nonisolated method in an actor.

Swift
actor Counter {
    nonisolated func [1]() -> String {
        return "Hello from nonisolated method"
    }
}
Drag options to blanks, or click blank then click option'
Agreet
Bincrement
Ccount
Dupdate
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that implies state change like 'increment' or 'update' in a nonisolated method.
2fill in blank
medium

Complete the code to mark the method as nonisolated in the actor.

Swift
actor Logger {
    [1] func log(message: String) {
        print(message)
    }
}
Drag options to blanks, or click blank then click option'
Anonisolated
Basync
Cisolated
Dstatic
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' or 'async' instead of 'nonisolated'.
3fill in blank
hard

Fix the error by adding the correct keyword to the method to make it nonisolated.

Swift
actor DataStore {
    func fetch() -> String {
        return "Data"
    }

    [1] func info() -> String {
        return "Info"
    }
}
Drag options to blanks, or click blank then click option'
Aisolated
Basync
Cnonisolated
Dstatic
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to mark the method as nonisolated causes isolation errors.
4fill in blank
hard

Fill both blanks to create a nonisolated static method inside an actor.

Swift
actor Service {
    [1] [2] func status() -> String {
        return "Running"
    }
}
Drag options to blanks, or click blank then click option'
Anonisolated
Bisolated
Cstatic
Dasync
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up 'isolated' and 'nonisolated' or forgetting 'static'.
5fill in blank
hard

Fill the blank to define a nonisolated async method inside an actor.

Swift
actor Network {
    [1] func fetchData() async -> String {
        return "Data fetched"
    }
}
Drag options to blanks, or click blank then click option'
Aisolated
Bnonisolated
Casync
Dstatic
Attempts:
3 left
💡 Hint
Common Mistakes
Putting async before func (invalid syntax; async belongs in the signature after ()).
Omitting nonisolated or using isolated.