Why does adding 'dynamic' fix my bad access issues?

后端 未结 2 1958
不思量自难忘°
不思量自难忘° 2020-12-28 13:23

I\'m having a strange issue that appeared with iOS 8 Beta 5 (this issue did not occur with previous versions).

I tried to create an empty project and try to replica

相关标签:
2条回答
  • 2020-12-28 13:50

    From Swift Language Reference (Language Reference > Declarations > Declaration Modifier)

    Apply this modifier to any member of a class that can be represented by Objective-C. When you mark a member declaration with the dynamic modifier, access to that member is always dynamically dispatched using the Objective-C runtime. Access to that member is never inlined or devirtualized by the compiler.

    Because declarations marked with the dynamic modifier are dispatched using the Objective-C runtime, they’re implicitly marked with the objc attribute.

    It means that your property/method can be accessed by Objective-C code or class. Normally it happens when you sub-classing a Swift class of Objective-C base class.

    0 讨论(0)
  • 2020-12-28 14:01

    This is from the prerelease Swift / Objective-C interoperability documentation:

    Implementing Core Data Managed Object Subclasses

    Core Data provides the underlying storage and implementation of properties in subclasses of the NSManagedObject class. Add the @NSManaged attribute before each property definition in your managed object subclass that corresponds to an attribute or relationship in your Core Data model. Like the @dynamic attribute in Objective-C, the @NSManaged attribute informs the Swift compiler that the storage and implementation of a property will be provided at runtime. However, unlike @dynamic, the @NSManaged attribute is available only for Core Data support.

    So, because of some of the Objective-C runtime features that Core Data uses under the covers, Swift properties need to be specially annotated.

    0 讨论(0)
提交回复
热议问题