Pood1331
Pood1331

Reputation: 235

Objective-C properties and instance variables

Problem

I want to create an interface with this signature, but without auto-synthesized instance variables:

@interface MyObject : NSObject
@property (nonatomic, copy) NSArray *values;
@end

Question:

Is it possible to prevent instance variable to be auto synthesized in .m @implementaion, as I want to implement my own getter and setter and I'm not going to use instance variable.

Reason:

The reason is that I don't want to have memory overhead, as data is going to be stored in plain bytes archive. At the same time I don't want users to know implementation issues and keep interface signature unchanged.

@implementation MyObject {
    NSData *_data
{
- (NSArray *)values
{
    // Generate NSArray from _data
}
- (void)setValues(NSArray *)values
{
    // Set _data from values
}
#pragma mark - Hidden init
- (id)initWithData:(NSData *)data
{
    // Set _data
}
@end

Upvotes: 0

Views: 101

Answers (3)

Hussain Shabbir
Hussain Shabbir

Reputation: 15035

If you dont wantbto create just create only instance variable.

@interface MyObject : NSObjet
 {
 NSArray *values;
}
@end

Upvotes: 0

Robert J. Clegg
Robert J. Clegg

Reputation: 7370

As others said - if you override the setter and getter - the compiler does't do anything else. So what you want.. is what you have typed out.

Upvotes: 0

FreeNickname
FreeNickname

Reputation: 7814

If you implement both getter and setter yourself, instance variables are not synthesized.

Upvotes: 2

Related Questions