带有@dynamic @properties 的超类的子类

Subclass for Superclass with @dynamic @properties

我正在使用 PAPreferences 来管理特定应用程序 preferences/defaults。它基本上是 NSUserDefaults 之上的一层,让我可以 read/write 向 NSUserDefaults 发送数据,就好像我只是在使用普通的 class.

基本设置是subclass PAPreferences,在你的subclass'.h文件中添加@properties并将它们声明为@dynamic 在.m文件中.

class 层次结构如下所示:

- PAPreferences
    - MyPreferences // all @properties defined here, and set in -init

我的项目构建了许多不同的目标,每次添加新目标时,我都必须复制以前的 MyPreferences 文件。很多代码是重复的,每次我想更改我的应用程序的首选项时,我都必须在许多不同的文件中这样做。

我想创建一个 class 层次结构,如下所示:

- PAPreferences
    - MyPreferencesBase // all @properties defined here
        - MyPreferences // all @properties set in -init

对于之前的 class 层次结构,当 MyPreferenes#init 为 运行 并且我设置了第一个实例变量(无法识别的选择器发送到实例)时,我的应用程序崩溃了。

如何为@properties 设置为@dyanmic 的超级class 编写子class?

这个问题对于动态实现属性的 superclass 来说并不普遍。具体到PAPreferences.

的实现

-[PAPreferences init], it examines the current class's properties by using the runtime function class_copyPropertyList()。该函数只记录了 return 在 class 中声明的属性,而不是它的 superclass(es)。因此,PAPreferences 看不到 superclass.

的动态属性

[PAPreferences init] 需要沿着超classes 的链向上走,处理所有classes 的属性。它会在到达自身时停止。它可能还需要调整在 +resolveInstanceMethod:.

中添加方法的 class