Writing a Custom Property Getter and Setter in Objective-C

In Objective-C, we use @property to provide managed access to the state of a class. The system provides basic management rules like thread locking and reference retainment. To provide custom access logic, you will need to write your own getter and setter methods. In this article, we will learn how to do that.

Let’s say that we have a Customer class and its name must always be in upper case. First, we declare the property in the interface:

@interface Customer
@property (nonatomic) NSString* name;

If you are writing your own accessor methods, the property must be set to be nonatomic (meaning, there is no thread locking).

Next, we do the implementation.

@implementation Customer

@synthesize name = _name;  //Must do this

//Setter method
- (void) setName:(NSString *)n {
    NSLog(@"Setting name to: %@", n);

    _name = [n uppercaseString];
//Getter method
- (NSString*) name {
    NSLog(@"Returning name: %@", _name);
    return _name;

- (void) test {
    self.name = @"Joan of arc";
    NSLog(@"Name is %@", self.name);


Here, we are declaring a member variable called _name that is mapped to the name property. The setter method is called setName. The getter method is just name.

7 thoughts on “Writing a Custom Property Getter and Setter in Objective-C

    • The statement – @synthesize name = _name; – binds the _name member variable to the name property. Normally, this is inserted by the compiler by default. I am not quite sure why we have to explicitly do this when we define a custom setter method.

  1. Take a look at apple’s documentation – https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW1

    “The compiler will automatically synthesize an instance variable in all situations where it’s also synthesizing at least one accessor method. If you implement both a getter and a setter for a readwrite property, or a getter for a readonly property, the compiler will assume that you are taking control over the property implementation and won’t synthesize an instance variable automatically.
    If you still need an instance variable, you’ll need to request that one be synthesized:”

    @synthesize property = _property;

    • Apart from this , there are other facilities they can do , Getter and Setter methods can be the best way for data model with string validation and operations like Capitalising or default string before assigning that value to a view object. If you are following pure MVC structure , you will able to see the data model does all processing and view just gets value from data model.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s