I want to access private property of a class from its category.
But to access private property, I have to redeclare the same private property in category.
If I don't redeclare, I get a compile error, Property '<property name>' not found on object of type '<class name> *'
.
Is this correct way to access private property of class from category?
And are there better ways to do this?
The following code is the code which private property is redeclared in category:
ClassA.h
@interface ClassA : NSObject
-(void)method1;
@end
ClassA.m
#import "ClassA.h"
// private property
@interface ClassA()
@property (nonatomic) NSString *s;
@end
@implementation ClassA
@synthesize s;
-(void)method1
{
self.s = @"a";
NSLog(@"%@", [NSString stringWithFormat:@"%@ - method1", self.s]);
}
@end
ClassA+Category.h
#import "ClassA.h"
@interface ClassA(Category)
-(void)method2;
@end
ClassA+Category.m
#import "ClassA+Category.h"
// redeclare private property
@interface ClassA()
@property(nonatomic) NSString *s;
@end
@implementation ClassA(Category)
-(void)method2
{
NSLog(@"%@", [NSString stringWithFormat:@"%@ - method2", self.s]);
}
@end
Is is good way to create a file(ClassA+Private.m) for private property and import it from ClassA.m and ClassA+Category.m:
ClassA+Private.m
@interface ClassA()
@property(nonatomic) NSString *s;
@end