1

I'm learning the objective C language and i ask a simple question, when i do that :

// ParentClass.h
@interface ParentClass : NSObject
@property (read, strong) NSString *parentPublicStr;
@end

// ParentClass.m
@interface ParentClass ()
@property (readwrite, strong) NSString *parentPrivateStr;
@end

@implementation ParentClass
@synthesize parentPublicStr;
@synthesize parentPrivateStr;
@end

// Subclass SubClass.h
@interface SubClass : ParentClass
- (void) test;
@end

@implementation SubClass
- (void) test
{
 // Its not possible to do that : [self setParentPrivateStr:@"myStrin"]
 // And for parentPublicStr, it is public property so not protected, because i can change the value
 // in main.c, and it's so bad..
}
@end

I would like create a property that is protected :x

Thx you. (Sorry for my english)

Marc Lamberti
  • 763
  • 2
  • 9
  • 24
  • possible duplicate of [Objective-C - Private vs Protected vs Public](http://stackoverflow.com/questions/4869935/objective-c-private-vs-protected-vs-public) – 一二三 Jun 12 '12 at 13:28

2 Answers2

2

Objective-C does not provide for protected methods/properties. See this question.

Edit: Also see this answer. You can still practice encapsulation by declaring the property in a class extension and including the extension in subclasses.

Community
  • 1
  • 1
tronbabylove
  • 1,102
  • 8
  • 12
0

You can manually create an ivar for the property as long as you use the same name prefixed with an underscore:

@interface ParentClass : NSObject
{
    @protected
    NSString* _parentPublicStr;
}
@property (read, strong) NSString *parentPublicStr;
@end

That makes the synthesized ivar for the property @protected (default is @private) and subclasses can then use the super class' ivar directly.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217