---
title: "What is the difference between retain & assign?"  
description: "What is the difference between retain & assign?"  
author: "Tarun Kumar"  
published: 2015-08-02  
updated: 2020-09-24  
canonical: https://www.mindstick.com/interview/12748/what-is-the-difference-between-retain-assign  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 1 minute  

---

# What is the difference between retain & assign?

**Assign** creates a reference from one object to another without increasing the source’s retain count.

```
if (obj1 !=
ob2)

{    [obj1 release];    obj1 = nil;    obj1 = obj2;

}
```

**Retain** creates a reference from one object to another and increases the retain count of the source object.

```
if (obj1 !=
obj2)

{   [obj1
release];   obj1 = nil;    obj1 = [obj2 retain];

}
```

## Answers

### Answer by Tarun Kumar

**Assign** creates a reference from one object to another without increasing the source’s retain count.

```
if (obj1 !=
ob2)

{    [obj1 release];    obj1 = nil;    obj1 = obj2;

}
```

**Retain** creates a reference from one object to another and increases the retain count of the source object.

```
if (obj1 !=
obj2)

{   [obj1
release];   obj1 = nil;    obj1 = [obj2 retain];

}
```


---

Original Source: https://www.mindstick.com/interview/12748/what-is-the-difference-between-retain-assign

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
