Browse Questions
  • Copy-on-write means a value type shares its underlying storage with copies until one of them is mutated — only then is a real copy made.
  • An actor is a reference type that protects its mutable state from concurrent access.
  • Sendable is a protocol that marks a type as safe to share across concurrency domains (actors, tasks).
  • UIStackView arranges views in a horizontal or vertical line and manages all constraints for you.

Answer: An actor is a reference type that protects its mutable state from concurrent access. The Swift compiler enforces that only one task accesses the actor's internals at a time.

Code Example:

actor ImageCache {
    private var cache: [URL: UIImage] = [:]

    func image(for url: URL) -> UIImage? {
        cache[url]
    }

    func store(_ image: UIImage, for url: URL) {
        cache[url] = image
    }
}

// Usage — must be awaited from outside the actor
let cache = ImageCache()
let img = await cache.image(for: url)
await cache.store(image, for: url)

Key Points:

  • Use actor for shared mutable state accessed from multiple tasks
  • @MainActor is a global actor that ensures code runs on the main thread
  • Actor methods are automatically async when called from outside