---
title: "Objective-C : Property and Synthesize Directive"  
description: "Objective C language provides default synthesis of those properties not declared @dynamic and not having user provided backing getter and setter metho"  
author: "Tarun Kumar"  
published: 2015-07-10  
updated: 2018-02-24  
canonical: https://www.mindstick.com/blog/10918/objective-c-property-and-synthesize-directive  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 2 minutes  

---

# Objective-C : Property and Synthesize Directive

Previously we learn how to release [object reference](https://www.mindstick.com/forum/376/system-nullreferenceexception-object-reference-not-set-to-an-instance-of-an-object): [Objective-C : NSAutoreleasePool class](https://www.mindstick.com/Blog/10917/Objective%20C%20NSAutoreleasePool%20class#.VZ_fLli6bIU)\

To remove the compiler error you can just declare the [instance variable](https://www.mindstick.com/forum/23344/multidimensional-array-of-nsinteger-as-instance-variable) yourself or request the compiler to do it for you with the @synthesize statement:

```
@synthesize myArray=_myArray;
```

Note that if you add the @synthesize statement you need to make sure that the name of the instance variable is consistent with the name used in the accessor method. In the getter method, we reference _myArray so we need to specify that name in the @synthesize statement. By default, if you do not specify a name for the instance variable the @synthesize statement will use the property name (without the leading underscore).

So the following would still generate a compiler error as it would create an instance variable named myArray and not the _myArray that we are using in the getter:

```
@synthesize myArray;  // equivalent to @synthesize myArray=myArray;
```

\

#### Customizing Accessors

Accessor methods can be customized using several property declaration [attributes](https://www.mindstick.com/articles/13105/2-attributes-that-make-your-odoo-ecommerce-theme-productive-and-engaging) (e.g., (copy)). Some of the most important attributes are:

**readonly -** Make the property read-only, meaning only a getter will be synthesized. By default, properties are read-write. This cannot be used with the setter attribute.

**getter=getterName -** Customize the name of the getter accessor method. Remember that the default is simply the name of the property.

**setter=setterName -** Customize the name of the setter accessor method. Remember that the default is set followed by the name of the property (e.g., setName ).

**nonatomic -** Indicate that the accessor methods do not need to be thread-safe. Properties are atomic by default, which means that Objective-C will use a lock/retain to return the complete value from a getter/setter. Note, however, that this does not guarantee [data integrity](https://www.mindstick.com/forum/160197/what-is-the-normalization-in-sql-server-explain-its-impact-on-data-integrity-and-performance) across threads-merely that [getters and setters](https://www.mindstick.com/forum/160071/what-are-getters-and-setters-in-javascript-classes-and-why-are-they-useful) will be atomic. If you're not in a threaded [environment](https://yourviews.mindstick.com/view/308/need-to-create-a-supportive-work-environment-for-women), non-atomic properties are much faster.

\

#### Dot Syntax

\

In addition to getter/setter methods, it's also possible to use [dot notation](https://www.mindstick.com/forum/33387/performance-difference-between-dot-notation-versus-method-call-in-objective-c) to access declared properties. For C# developers, this should be much more familiar than Objective-C's square-bracket messaging syntax:

```
Person *frank = [[Person alloc] init];
frank.name = @"Frank";    // Same as [frank setName:@"Frank"];
NSLog(@"%@", frank.name); // Same as [frank name];
```

**Note this is just a [convenience](https://www.mindstick.com/forum/33530/convenience-vs-init-method-memory-usage)-**it translates directly to the **getter/setter** methods described previously. Dot notation cannot be used for [instance methods](https://www.mindstick.com/forum/161894/what-s-the-difference-between-staticmethod-classmethod-and-instance-methods).

---

Original Source: https://www.mindstick.com/blog/10918/objective-c-property-and-synthesize-directive

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
