ashish
ashish

Reputation: 474

hide @property in objective C

How to hide @property(retain, nonatomic)NSString*str in some class?

Upvotes: 1

Views: 1398

Answers (2)

Count Chocula
Count Chocula

Reputation: 1041

You can use a feature called “categories,” which allows you to extend an existing class by adding properties and methods to it.

Typically, you use an empty category inside your class's .m file for private methods and properties, and define a separate named category in an external .h file for protected members. Categories are actually quite interesting in that they allow you to override existing properties. So, for example, you can define a property as read-only in your .h file:

@interface Whatever {
    NSObject *aValue;
}

@property (nonatomic,retain,readonly) NSObject *aValue;

@end

And then you can make it read/write for your own private use in an empty category inside your .m file:

@interface Whatever()
  @property (nonatomic,retain) NSObject *aValue;
@end

You can find more about categories here.

Upvotes: 2

Eiko
Eiko

Reputation: 25632

If you want to hide it from being visible in the .h file, consider using a private class extension in the .m file:

@interface YourClass ()

@property(retain, nonatomic)NSString*str;

@end

Upvotes: 6

Related Questions