nyancoder

Actor-isolated instance method '' can only be referenced on 'self' 본문

Short answer

Actor-isolated instance method '' can only be referenced on 'self'

nyancoder 2021. 7. 1. 23:47

문제 상황

Swift의 Actor에서 다른 Actor의 함수를 접근할 때 발생하는 오류 중 하나로 아래와 같이 발생한다.

해결 방안

해당 함수를 async로 선언하면 발생하지 않는다.

actor TestActor {
    func foo(testActor: TestActor) async {
        await testActor.bar()
    }

    func bar() {
    }
}

또는 아래와 같이 async 블럭으로 감싸도 된다.

actor TestActor {
    func foo(testActor: TestActor) {
        async {
            await testActor.bar()
        }
    }

    func bar() {
    }
}

 

연관 자료: WWDC 2021 - Protect mutable state with Swift actors

 

WWDC 2021 - Protect mutable state with Swift actors

원본 영상: https://developer.apple.com/videos/play/wwdc2021/10133/ 이 장에서는 Swift에서 새로 지원되는 Actor의 필요성과 활용에 대해서 알아봅니다. 기존 방식의 문제점 둘 이상의 스레드에서 동일 데이터..

nyancoder.tistory.com

 

Comments