Swift isa pointer remapping or other supported method swizzling

前端 未结 2 1401
北恋
北恋 2020-12-30 07:17

Do Swift classes have something like an isa pointer that can be remapped?

We\'ve seen that Swift uses a more static method dispatch than objective-C, which (unless

2条回答
  •  时光说笑
    2020-12-30 08:14

    I can't answer your question about swift "isa" equivalent, but I think I know part of the answer to your underlying question.

    Property Observers seem to be the built-in means for the Observer Pattern. Instead of runtime discovery of "type" (RTTI, what-have-you) it is woven in explicitly.

    From 'The Swift Programming Language' page 345:

    Property observers observe and respond to changes in a property's value. Property observers are called every time a property's value is set, even if the new value is the same as the property's current value.

    You can add property observers to any stored properties you define, apart from lazy stored properties. You can also add property observers to any inherited property (whether stored or computed) by overriding the property within a subclass.

    You have the option to define either or both of these observers on a property:

    • willSet is called just before the value is stored.
    • didSet is called immediately after the new value is stored.

    I am not sure how this is all going to work out, but I am intrigued.

    Relying on run-time type discovery also seems to run counter to strong static type orthodoxy.

提交回复
热议问题