Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properties for Class and Its Subclasses Only

Is it possible to define properties that are only available to the class they are defined in, and that class's subclasses?

Stated another way, is there a way to define protected properties?

like image 733
Jay Haase Avatar asked Nov 24 '10 07:11

Jay Haase


2 Answers

Technically, no. Properties are really just methods, and all methods are public. The way we "protect" methods in Objective-C is by not letting other people know about them.

Practically, yes. You can define the properties in a class extension, and still @synthesize them in your main implementation block.

like image 184
Dave DeLong Avatar answered Nov 02 '22 07:11

Dave DeLong


This is possible by using a class extension (not category) that you include in the implementation files of both the base class and subclasses.

A class extension is defined similar to a category, but without the category name:

@interface MyClass ()

In a class extension, you can declare properties, which will be able to synthesize the backing ivars (XCode > 4.4 automatic synthesis of the ivars also works here).

In the extension class, you can override/refine properties (change readonly to readwrite etc.), and add properties and methods that will be "visible" to the implementation files (but note that the properties and methods aren't really private and can still be called by selector).

Others have proposed using a seperate header file MyClass_protected.h for this, but this can also be done in the main header file using #ifdef like this:

Example:

BaseClass.h

@interface BaseClass : NSObject

// foo is readonly for consumers of the class
@property (nonatomic, readonly) NSString *foo;

@end


#ifdef BaseClass_protected

// this is the class extension, where you define 
// the "protected" properties and methods of the class

@interface BaseClass ()

// foo is now readwrite
@property (nonatomic, readwrite) NSString *foo;

// bar is visible to implementation of subclasses
@property (nonatomic, readwrite) int bar;

-(void)baz;

@end

#endif

BaseClass.m

// this will import BaseClass.h
// with BaseClass_protected defined,
// so it will also get the protected class extension

#define BaseClass_protected
#import "BaseClass.h"

@implementation BaseClass

-(void)baz {
    self.foo = @"test";
    self.bar = 123;
}

@end

ChildClass.h

// this will import BaseClass.h without the class extension

#import "BaseClass.h"

@interface ChildClass : BaseClass

-(void)test;

@end

ChildClass.m

// this will implicitly import BaseClass.h from ChildClass.h,
// with BaseClass_protected defined,
// so it will also get the protected class extension

#define BaseClass_protected 
#import "ChildClass.h"

@implementation ChildClass

-(void)test {
    self.foo = @"test";
    self.bar = 123;

    [self baz];
}

@end

When you call #import, it basically copy-pastes the .h file to where you are importing it. If you have an #ifdef, it will only include the code inside if the #define with that name is set.

In your .h file, you don't set the define so any classes importing this .h wont see the protected class extention. In the base class and subclass .m file, you use #define before using #import so that the compiler will include the protected class extension.

like image 40
d4n3 Avatar answered Nov 02 '22 06:11

d4n3