TIL_230111

애드·2023년 1월 29일

TIL

목록 보기
26/26

Escaping closure captures mutating 'self' parameter 에러

뜨는 이유:
The reason you don't see the "Escaping closure captures mutating 'self' parameter" error when you changed your view model to a class is because classes are reference types, while structs are value types.

When you pass a struct as a parameter, the struct's value is copied, and the copy is passed to the function. If the function modifies the copy, it doesn't affect the original struct. However, when you pass a class, you are passing a reference to the object in memory, and any modifications made to the object will affect the original as well.

When the closure is called, in case of struct the reference to self is captured and if the closure modifies the self, it modifies the copy of it, not the original, this is why you were getting the "Escaping closure captures mutating 'self' parameter" error. However, in case of class, the closure is referencing the same object in memory, any modification made to the class object inside the closure will be retained to the outside as well.

In other words, since the class instance is being passed by reference and since the closure can modify the state of the class instance, there is no need to use the [weak self] pattern as there is no risk of a strong reference cycle, as the class will be destroyed when there is no strong reference to it.

struct vs. class
https://showcove.medium.com/swift-struct-vs-class-1-68cf9cbf87ca

derived data 삭제: settings -> locations

dump(URLRequest)를 하면 다른 것들 보다 디버깅하기 편한 듯

Test doubles - mock, stub, dummy, fake, spy
https://jiseobkim.github.io/swift/2022/02/06/Swift-Test-Double(부제-Mock-&-Stub-&-SPY-이런게-뭐지-).html
https://codinghack.tistory.com/92

의존성 주입, DIP (의존 관계 역전 법칙)
https://80000coding.oopy.io/68ee8d89-5d05-449d-87e2-5fba84d604ca

profile
2차전직 개발자가 되자

0개의 댓글