---
title: "Objective-C : Attributes of @Property Directive"  
description: "@property offers a way to define the information that a class is intended to encapsulate. If you declare an object/variable using @property, then that"  
author: "Anonymous User"  
published: 2015-07-17  
updated: 2018-03-13  
canonical: https://www.mindstick.com/blog/10927/objective-c-attributes-of-property-directive  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 3 minutes  

---

# Objective-C : Attributes of @Property Directive

@property offers a way to define the information that a class is intended to encapsulate. If you declare an object/variable using @property, then that object/variable will be accessible to other classes importing its class. If you declare an object using @property in the [header file](https://answers.mindstick.com/qa/104712/how-to-include-a-header-file), then you have to synthesize it using @synthesize in the [implementation](https://www.mindstick.com/forum/33764/how-to-implementation-of-class-in-c-sharp) file.

\

##### List of attributes of @property :

· atomic.

· nonatomic.

· retain.

· copy.

· readonly.

· readwrite.

· assign.

· strong.

· weak

atomic : It is the default behaviour. If an object is [declared as](https://www.mindstick.com/interview/2711/what-if-the-main-method-is-declared-as-private) atomic then it

\

becomes thread-safe. Thread-safe means, at a time only one thread of a

\

particular instance of that class can have the control over that object.

##### Example :

\

```
@property NSString *name; //by default atomic@property (atomic)NSString *name; // explicitly declared atomic
```

nonatomic: It is not thread-safe. You can use the nonatomic property attribute to specify that synthesized accessors simply set or return a value directly, with no guarantees about what [happens if](https://www.mindstick.com/forum/159800/what-happens-if-the-ldf-file-grows-too-large) that same value is accessed simultaneously from different threads. For this reason, it’s faster to access a nonatomic property than an atomic one.

```
@property (nonatomic)NSString *name;  
```

**retain**: is required when the attribute is a pointer to an object.The setter method will increase retain count of the object, so that it will occupy memory in autorelease pool.

```
@property (retain)NSString *name;
```

copy: If you use copy, you can't use retain. Using copy instance of the class

\

will contain its own copy.

Even if a mutable string is set and subsequently changed, the instance

\

captures whatever value it has at the time it is set. No setter and getter

\

methods will be synthesized.

\

```
@property (copy) NSString *name; NSMutableString *nameString = [NSMutableString stringWithString:@"Liza"];    xyzObj.name = nameString;    [nameString appendString:@"Pizza"];
```

readonly: [If you don](https://www.mindstick.com/forum/159346/how-is-the-exception-handled-if-you-don-t-catch-it)'t want to allow the property to be changed via setter method, you can declare the property readonly.

```
@property (readonly) NSString *name;
```

readwrite: is the default behaviour. You don't need to specify readwrite attribute explicitly.

\

```
@property (readwrite) NSString *name;
```

assign: will generate a setter which assigns the value to the [instance variable](https://www.mindstick.com/forum/23344/multidimensional-array-of-nsinteger-as-instance-variable) directly, rather than copying or retaining it. This is best for primitive types like NSInteger and CGFloat, or objects you don't directly own, such as delegates.

```
@property (assign) NSInteger year;
```

strong: is a replacement for retain.

\

```
@property (nonatomic, strong) AVPlayer *player;
```

unsafe_unretained: There are a few classes in [Cocoa and Cocoa](https://www.mindstick.com/interview/23135/what-is-difference-between-cocoa-and-cocoa-touch) Touch that don’t yet support weak references, which means you can’t declare a weak property or weak [local variable](https://www.mindstick.com/forum/158997/can-a-local-variable-s-memory-be-accessed-outside-its-scope) to keep track of them. These classes include NSTextView, NSFont and NSColorSpace,etc. If you need to use a [weak reference](https://www.mindstick.com/forum/23340/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java) to one of these classes, you must use an unsafe reference.

An unsafe reference is similar to a weak reference in that it doesn’t keep its related object alive, but it won’t be set to nil if the [destination](https://yourviews.mindstick.com/story/1451/gorgeous-locations-for-destination-weddings-in-india) object is deallocated.

\

```
@property (unsafe_unretained) NSObject *unsafeProperty; 
```

\

##### Weak

\

weak is similar to strong except that it won't increase the reference count by 1. It does not become an owner of that object but just holds a reference to it. If the object's reference count drops to 0, even though you may still be pointing to it here, it will be deallocated from memory.

---

Original Source: https://www.mindstick.com/blog/10927/objective-c-attributes-of-property-directive

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
