lppier
lppier

Reputation: 1987

iOS Setter Getter

I tried to assign a value to recordingStatus - ie recordingStatus = 1 But it doesn't go into the setter which i want some custom code.. what's wrong with my code?

Thanks.

Pier.

In file.h

@property (strong, nonatomic) IBOutlet UILabel *recordingStatusText;
@property (nonatomic)int recordingStatus;
....

In file.m

/* -------------------- Property Setter and Getters ----------------------*/
@synthesize recordingStatus;

- (int) getRecordingStatus {
    return recordingStatus;
}

- (void) setRecordingStatus:(int)status 
{
[_recordingStatusText setText: @"Just testing!"];
recordingStatus = status; 
}

Upvotes: 0

Views: 2204

Answers (2)

MJN
MJN

Reputation: 10808

To set and get your property, you should use self.property = newValue;.

OVERRIDING SETTERS AND GETTERS

For getters you don't need to write 'get' in the method signature. So, your getter method uses the wrong name. If you want to override it, the method should be

-(int) recordingStatus {
    // Custom Getter Method
    return _recordingStatus;
}

In the case of ints, Objective-c wants to see your setter and getter methods in the format of

-(void)setValue:(int)newValue;
-(int)value;

Upvotes: 4

Chris Gummer
Chris Gummer

Reputation: 4782

Can you show the code where you call the setter? I'm assuming you're accessing the ivar directly by doing something like this (assuming your ivar is named recordingStatus):

recordingStatus = 1

Instead try this:

self.recordingStatus = 1

Upvotes: 2

Related Questions