As mentioned by Mindaugas in one of the comment, you can use class extension for this purpose.
Class extensions are often used to extend the public interface with additional private methods or properties for use within the implementation of the class itself.
For example
.h contains
@interface Person : NSObject
@property(nonatomic, copy) NSString *firstName;
@property(nonatomic, copy) NSString *lastName;
- (void)setDateOfBirth:(NSDate *)date;
@end
.m will have
@interface Person()
@property(nonatomic, strong) NSDate *dateOfBirth;
- (void)calculateMyAge;
@end
@implementation Person
- (void)setDateOfBirth:(NSDate *)date
{
self.dateOfBirth = date;
//Some more code goes here
}
- (void)calculateMyAge
{
//Do something with self.dateOfBirth here
}
@end
Here we made @property
dateOfBirth and a method calculateMyAge private to the interface Person and implemented it under @implementation
block.
@interface Person()
is nothing but class extension (or nameless category or anonymous category).
Now coming back to original question, you want to make it public to your framework and private for outside world.
Let's say you want Person class to have a method that will return full name (first and last name appended). However you want for internal purpose of your framework.
You can create a separate header file for this.
Person+InternalHeader.h
which will have declaration for the method
@interface Person()
- (NSString *)fullName;
@end
You can import
#import "Person+InternalHeader.h"
into your Person.m
file and under @implementation
block you can implement body for the method.
@implementation Person
//other code
- (NSString *)fullName
{
return [self.firstName stringByAppendingFormat:@" %@",self.lastName];
}
@end
You can always import Person+InternalHeader.h into your other classes and make use of fullName method.
Why extension and not category?
Extensions enable you to override the property or add new property to the existing parent class. Also you can always override the default behaviour of method defined in extension, in child class. With category, it simply adds the method to your existing object.
For example, lets there is class Doctor which inherits from class Person.
@interface Doctor : Person
Now you want the fullName
method to return "Dr." prepended. You can simply override the fullName
behaviour in your Doctor.m
file.
@implementation Doctor
- (NSString *)fullName
{
NSString *fullName = [super fullName];
return [@"Dr. " stringByAppendingString:fullName];
}
@end