궁금한 것은 총 3가지다.
1. dynamic var가 대체 뭐임?
2. dynamic 키워드 앞에 @objc 붙이기도 하고 아니기도 하던데 뭐임? 내가 알던 @objc는 대체 무엇이었던 거임?
3. @NSManaged 얜 또 뭐임...
아래에다가 차근차근 정리해보자~
dynamic var와 죽지 않고 살아돌아온 Method Dispatch
그렇다. 사실 이 글은 앞서 작성했던 final 키워드 글과 연관성이 있다. 그 중에서도 Method Dispatch 부분과.
⬇️⬇️⬇️ 이전 글 보러가기 ⬇️⬇️⬇️
https://be-beee.tistory.com/58
[Swift] final (feat. Method Dispatch)
https://github.com/apple/swift/blob/main/docs/OptimizationTips.rst#advice-use-final-when-you-know-the-declaration-does-not-need-to-be-overridden GitHub - apple/swift: The Swift Programming Language The Swift Programming Language. Contribute to apple/swift
be-beee.tistory.com
차라리 Method Dispatch를 주제로 글을 작성하는 것이 더 나았을 것이라는 후회가 살짝 밀려오는 중이다..
dynamic 키워드를 붙이면 Swift 런타임이 아닌, Objective-C 런타임에 실행된다고 함.
+ KVO 구현 시 dynamic 키워드를 붙여 추적할 값을 지정함!!
🙋♀️ 스위프트 런타임과 Objective-C의 런타임은 무슨 차이가 있나용?
는 내가 궁금했던 부분
https://nshipster.com/swift-objc-runtime/
Swift & the Objective-C Runtime
Even when written without a single line of Objective-C code, every Swift app executes inside the Objective-C runtime, opening up a world of dynamic dispatch and associated runtime manipulation. To be sure, this may not always be the case—Swift-only frame
nshipster.com
뭔가 Swift 런타임이 아닌, Objective-C 런타임에서 동작하는 이유가 설명되어 있을 줄 알았지만, Realm 공식 문서에도 그냥 dynamic var를 써줘야한다~ 라고만 되어 있었다고 한다.
Realm model properties need the dynamic var attribute in order for these properties to become accessors for the underlying database data.
There are two exceptions to this: List and RealmOptional properties cannot be declared as dynamic because generic properties cannot be represented in the Objective-C runtime, which is used for dynamic dispatch of dynamic properties, and should always be declared with let.
출처: https://stackoverflow.com/a/33177633
코드를 뜯어봐야하나 하는 생각이 스쳐 지나갔지만 Objective-C 코드를 감당할 자신이 없음ㅋㅋ;;
@objc
위의 링크에 포함되어 있는 답변인데, Swift3에서는 dynamic var 로 표현했지만, Swift4 이후부터는 @objc dynamic var 로 표현한단다. 머쓱...
조금 더 찾아봤더니, dynamic 키워드 자체가 Objective-C의 @dynamic에서 따온 거라 의미를 명확하기 위해 이렇게 바뀐 듯함
In Swift 3, we declared our property like this
dynamic var Name : String = ""
In Swift 4, we declared our property like this
@objc dynamic var Name : String = ""
출처: https://stackoverflow.com/a/47955433
기본적으로는 Swift가 Objective-C와 런타임에서 상호작용 해야할 때 사용하는 키워드다.
@NSManaged
@NSManaged is syntactic sugar that is a more narrow version of dynamic.
출처: https://stackoverflow.com/a/31357615
dynamic 키워드와 유사하게 런타임에서 동작하지만, 좀 더 좁은 개념으로 동작하는 슈거 키워드..라고 함
출처에서는 CoreData를 사용할 때 쓰이는 키워드라고 되어 있는데, 내가 발견했던 예시에서는 CoreData를 사용하지 않음에도 해당 키워드를 쓰고 있어서 이런 경우는 어떻게 된 건지 궁금함.
동작 자체는 dynamic 키워드와 유사하니까 상관 없는 걸까 싶기도..
공부하게 된 이유
쓸데없이 본문보다 길어서 그냥 밑으로 옮겼습니다..
Realm을 사용하면서 알게된 키워드 dynamic
사실 사용할 때는 별생각없이 사용하던 키워드인데 다른 프로젝트 하면서도 발견해버려서 더 이상 무시할 수 없게 되어버렸다.
어쩌다가 또 발견하게 되었느냐 한다면 대답하는 것이 인지상정
Animating Custom CALayer Properties in Swift - And What To Check For When They Aren’t Working - Tapadoo
Creating custom CALayers in your iOS apps can be very useful, and animating them can add some polish to the final result. CALayers are everywhere in UIKit – every UIView has a layer to draw its contents on. Layers have been around forever, and you’ll f
tapadoo.com
iOS: Animating a circle slice into a wider one
Core-Animation treats angles as described in this image: (image from http://btk.tillnagel.com/tutorials/rotation-translation-matrix.html) EDIT: Adding an animated gif to explain better what I'm ne...
stackoverflow.com
그것은 CAAnimation의 keyPath를 커스텀하는 과정에서 발견하게 된 것이다.. 같은 결과를 내고 있는 구현이지만 위의 링크에서는 @objc dynamic var 키워드를 사용하고, 아래는 @NSManaged 키워드를 사용한다.
사실 keyPath를 커스텀하는 일이 얼마나 자주 있겠느냐만은
Realm은 은근히 여기저기 자주 쓰이게 될 것 같으니 알아보기로
그리고 이 글을 쓰다보니 드는 의문인데 dynamic 키워드를 사용할 때 @objc 키워드도 같이 쓰기도 하는 것 같다.
이건 또 왜일까? @objc 는 단순히 옵젝씨라는 것을 알리는 키워드가 아닌 걸까..
모르겠으니까 이것도 정리해두기로 함
또또 궁금한 @NSManaged 키워드도 함께 정리해두자!하는 마음에 이렇게 작성하게 되었읍니다..
참고
https://realm.io/docs/swift/latest/#property-attributes
https://myssun0325.tistory.com/37
https://stackoverflow.com/questions/33177283/dynamic-properties-in-realm
https://zeddios.tistory.com/296
'Swift' 카테고리의 다른 글
[Swift] sync, async, serial, concurrent (0) | 2023.01.17 |
---|---|
[Swift] method swizzling (0) | 2023.01.11 |
[Swift] Safe Index (0) | 2023.01.07 |
[Swift] final (feat. Method Dispatch) (0) | 2023.01.05 |
#0. 목적 (0) | 2020.04.14 |