saikamesh
saikamesh

Reputation: 4629

Objective-C Package level Property

I am creating an iPhone custom framework which should be able to be integrated into any iPhone app.

I have created some properties in one of the public header files of my framework. I want to give @package level access to those properties, so that those properties can be accessed only with in the classes inside the framework.

I do not want the user to use those properties.

Please tell me whether doing this is possible. If yes, please give me some idea on how to achieve this!.

Upvotes: 3

Views: 346

Answers (2)

Taum
Taum

Reputation: 2551

First you should know that there is no way to completely forbid a user of your library to call a method. Even if you don't declare it in your header, a user could declare it on its own and use it. He would still have to find out the name of your property though, for instance by running classdump on your library.

Therefore in Objective-C, we make properties private by not declaring them in the header (which is the public part of your class), but by declaring them in the implementation (which is the "private" part of your class).

If you create another header which contains a category on your class, you can add some properties to it that will not be in the main header - so not on the public declaration of your class - but that can still be imported by other classes of your library that know about this header.

For instance:

MyClass+SecretProperties.h:

@interface MyClass ()
@property (strong) NSString *secretString;
@end

MyClass.m:

#import "MyClass.h"
#import "MyClass+SecretProperties.h"
@implementation MyClass
@synthesize secretString; // Depending on your runtime, you may not even need to this - properties are auto-synthesized in the latest SDKs.
…
@end

OtherClass.m:

#import "MyClass.h"
#import "MyClass+SecretProperties.h"

// Now you can use secretString on instances of MyClass

Then since you only export MyClass.h with your library, users have no idea that there is a secretString property :) This is the closest you can get to a @package scope AFAIK.

Upvotes: 4

NSCry
NSCry

Reputation: 1652

If you want to make those property as private then use below things. In your .m file use extension characteristics of objective c.

@interface InitialViewController ()
//declare your property here
@end 

Upvotes: 0

Related Questions