1

I have a read only property

@property (nonatomic, readonly)  NSInteger   keepalive;

With a getter method

-(NSInteger)keepalive
{
    return _keepalive / 1000;
}

ERROR - Use of undeclared identifier

And trying to set a value in init method

- (instancetype)init
{
    self = [super init];

    if (self)
    {
        _keepalive = 25000;
    }
    return self;
}

ERROR - Use of undeclared identifier

Couldn't understand the reason why and what's the right way to have a readonly property, with a getter.

EDIT: I wouldn't want to add synthesize as suggested in the comment, since it's not needed anymore - as stated here AND I got the correct answer below. Thank you André Slotta!

Community
  • 1
  • 1
Gal
  • 1,582
  • 2
  • 14
  • 30
  • @originaluser2 - I disagree with your 'duplicated' marking. Please read my editing. Thanks – Gal Apr 12 '16 at 13:27
  • @Josh Caswell- I disagree with your 'duplicated' marking. Please read my editing. Thanks – Gal Apr 12 '16 at 13:27

1 Answers1

1

in your case you could redeclare the property as readwrite in your implementation file:

.h:

@property (nonatomic, readonly)  NSInteger keepalive;

.m:

@property (nonatomic, readwrite)  NSInteger keepalive;

This lets you use compiler-generated atomic setters and getters inside of your implementation, but not expose the setters to the rest of the world.

(see https://www.bignerdranch.com/blog/property-values/)

André Slotta
  • 13,774
  • 2
  • 22
  • 34