Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: access private propertys in a inherited class

I'm trying to access private properties on a inherited class. Is that possible with that naming convention apple gave me?

Header:

#import <Foundation/Foundation.h>

@interface FooBar : NSObject
-(void)doSomeThingWithThisNumber:(NSInteger)aNumber;
@end

Implementation:

#import "FooBar.h"


@interface FooBar()
@property NSInteger myPrivateFoo;
@end


@implementation FooBar
@synthesize myPrivateFoo = _myPrivateFoo;

-(void)doSomeThingWithThisNumber:(NSInteger)aNumber
{
    _myPrivateFoo = aNumber;
}

@end

if i inherit this class to a new one i can't access _myPrivateFoo. Is there another way to do it instead of the declaration in the header file?

like image 977
lenn1 Avatar asked Nov 23 '11 21:11

lenn1


1 Answers

In the implementation of the subclass, simply put the private category declaration before your main @interface. This will prevent compilation errors because the compiler will know that the property exists. This is what I mean by "the private category declaration":

@interface FooBar()
@property NSInteger myPrivateFoo;
@end
like image 181
Alex Nichol Avatar answered Sep 26 '22 07:09

Alex Nichol