我知道通过将它们放在该类的实现(.m)文件中声明的类中的未命名类别中,可以声明一个类上的私有属性.这不是我想做的.
我在一个类上处理一个命名类,为该类添加了一些功能.对于这个功能,这将有助于我在私人财产中使用我的类别 – 所以通常的实现方式(上面描述)对我来说似乎不起作用.还是呢请启发我!
解决方法
在类别的实现文件中,声明另一个类别,并将其称为类似MyCategoryName_Private的类,并在其中声明您的私有属性.提供使用关联对象的-propertyName和-setPropertyName:方法的实现.
例如,您的实现文件可能如下所示:
#import "SomeClass+MyCategory.h"
#import <objc/runtime.h>
@interface SomeClass (MyCategory_Private)
@property (nonatomic,strong) id somePrivateProperty;
@end
@implementation SomeClass (MyCategory_Private)
static void *AssociationKey;
- (id)somePrivateProperty
{
return objc_getAssociatedObject(self,AssociationKey);
}
- (void)setSomePrivateProperty:(id)arg
{
objc_setAssociatedObject(self,AssociationKey,arg,OBJC_ASSOCIATION_RETAIN);
}
@end
@implementation SomeClass (MyCategory)
// implement your publicly declared category methods
@end

