Friday, April 19, 2024
HomeProgrammingFixing Swift Actors and Delegate Error on Xcode 14 | by Max...

Fixing Swift Actors and Delegate Error on Xcode 14 | by Max Kalik | Sep, 2022


In case you up to date Xcode to model 14 you may get an error about mutating actor-isolated property in your venture. Let’s repair it.

I’m not going to inform you once more about Actors from scratch, what’s it and the way it works. Since Apple has launched Swift 5.5 there are tons of cool articles about them. In case you nonetheless don’t use actors then it’s time to have a look. I guess it’ll come a time when you’ll change a few of your courses to actors.

This specific article is a couple of particular potential subject whereas utilizing actors with delegates after updating Xcode with model 14. It sounds a bit trivial however I believe it might be helpful. Let’s get began.

Let’s assume we have now class SomeClass with a delegate:

SomeClass with delegate

And SomeViewModel — in there we going to make use of our SomeClass:

SomeView mannequin with SomeClass

As you may see, we did a primitive manipulation by setting delegate to self and conforming view mannequin with SomeClassDelegate.So, let’s print out some outcomes from it.

let someViewModel = SomeViewModel()
someViewModel.begin()
// Printed: some class did begin

Okay. Now it’s time to refactor our class with an actor. The way it might be (and perhaps you could have completed it already in your venture on this comparable approach):

In the identical approach, let’s use it in our view mannequin. After all, since we have now a take care of an actor we want Duties.

In case you are nonetheless utilizing Xcode 13 you in all probability gained’t see any warnings on this implementation. However when you have up to date already Xcode with model 14, more than likely you’re going to get this error:

Actor-isolated property ‘delegate’ cannot be mutated from a non-isolated context

At first sight, it’s complicated, particularly: “No async operations happen inside await expression” when the delegate is an actor’s property. Additionally, it might be a query — if we await to alter a property from exterior, why can we see this message about isolation?

In actuality, the errors each make sense and look bizarre a bit of bit. As we all know — the bottom thought of the actors is to isolate properties. It means you can not simply replace property from exterior. So, in all probability, these errors educate us how one can use actors correctly and the updating of the property delegate ought to occur solely within the actor SomeActor.

An inexpensive answer is so as to add an extra technique. This technique will set the delegate with a brand new worth.

setDelegate technique

Inside the Process in view mannequin exchange this:

// await someActor.delegate = self
await someActor.setDelegate(self)

Anticipated consequence:

let someViewModel = SomeViewModel()
someViewModel.begin()
// Printed: some actor did begin

I wouldn’t write this text if the error wasn’t so sudden. I see already some questions seem in Stackoverflow or in Swift discussions and I requested the identical. If this actor-isolated property error is just about instructing us how one can use actors correctly, why can’t or not it’s solely a warning?

Thanks for studying.

Wish to Join?Do not hesitate to comply with me on Twitter.I admire any solutions or feedback concerning my analysis and articles.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments