Hello
I'm getting this error in my code and I don't know why. Can somebody explain this or point me at a help article.
import UIKit
var greeting = "Hello, playground"
let imageView = UIImageView()
imageView.image = UIImage(named: "image")
Utils.getImageSize(view: imageView)
class Utils {
static func getImageSize(view imageView: UIImageView) {
let image = imageView.image
print("Image size = \(image?.size ?? CGSize.zero)")
}
}
The error is as follows:
Cheers Murray
It’s hard to say for sure what’s going on without seeing the full playground. I suspect you have a class like this:
class MyImageView: UIView {
var image: UIImage? = nil
func getImage() -> UIImage {
fatalError()
}
}
If so, I can explain this:
I don't understand why it needs to run on the main thread?
MyImageView
is a subclass of UIView
and UIView
is main actor isolated. Thus the getImage()
method is also isolated to the main actor. In your original implementation, Utils.getImageSize(…)
wasn’t isolated to the main actor, and thus could be running on any thread. Thus you have non-isolated code calling isolated code, and the compiler complains.
Isolating Utils
to the main actor resolves this problem.
In the long term I expect that UIView
playgrounds will use approachable concurrency to default everything to the main actor, and this sort of code will just work out the box.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"