ChatGPT解决这个技术问题 Extra ChatGPT

Objective C - Assign, Copy, Retain

I'm new to Objective C. I have basic knowledge in C, including the concept of pointers. I have two basic questions:

Can someone explain the difference between assign,copy, and retain with some analogy? How do you handle a function which returns pointer variable, and how do you perform messaging through a return pointer?


J
Joshua Nozzi

Updated Answer for Changed Documentation

The information is now spread across several guides in the documentation. Here's a list of required reading:

Cocoa Core Competencies: Declared property

Programming with Objective-C: Encapsulating Data

Transitioning to ARC Release Notes

Advanced Memory Management Programming Guide

Objective-C Runtime Programming Guide: Declared Properties

The answer to this question now depends entirely on whether you're using an ARC-managed application (the modern default for new projects) or forcing manual memory management.

Assign vs. Weak - Use assign to set a property's pointer to the address of the object without retaining it or otherwise curating it; use weak to have the property point to nil automatically if the object assigned to it is deallocated. In most cases you'll want to use weak so you're not trying to access a deallocated object (illegal access of a memory address - "EXC_BAD_ACCESS") if you don't perform proper cleanup.

Retain vs. Copy - Declared properties use retain by default (so you can simply omit it altogether) and will manage the object's reference count automatically whether another object is assigned to the property or it's set to nil; Use copy to automatically send the newly-assigned object a -copy message (which will create a copy of the passed object and assign that copy to the property instead - useful (even required) in some situations where the assigned object might be modified after being set as a property of some other object (which would mean that modification/mutation would apply to the property as well).


Thanks dude I read the basics from Apple Dev , on my present development Having a hard time in memory management "crashed in objc_msgSend()" So i just thought of revising my understanding on properties. stackoverflow.com/questions/4506205/…
Just a heads up, the link into the answer goes to a generic "Page not found" on apples site. Then it redirects to the "Mac Developer Library" This SO answer has useful info: stackoverflow.com/questions/2255861/…
Kind of a difficult one to update because the documentation has changed and it's no longer in one convenient place. Feedback sent to Apple doc team.
L
Larry Hipp

The Memory Management Programming Guide from the iOS Reference Library has basics of assign, copy, and retain with analogies and examples.

copy Makes a copy of an object, and returns it with retain count of 1. If you copy an object, you own the copy. This applies to any method that contains the word copy where “copy” refers to the object being returned. retain Increases the retain count of an object by 1. Takes ownership of an object. release Decreases the retain count of an object by 1. Relinquishes ownership of an object.


Thanks for your reply.Could you please explain 1.ClassA have a property called - (Class B*)functionName; 2.ClassB have method – (void)setHeight; 3.ClassC interface I have declared ClassA *tempA as retain. 4.Class C implementation in the constructor I have this statement ClassB *tempB = tempA.functionName; 5.When I try to send a message (tempB. setHeight;) on the next line of tempB initialization my program works fine.
6.putting the same statement(tempB. setHeight;) into some function and calling the same function my program get Crashed in objc_msgSend().
z
zx485
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"First",@"Second", nil];
NSMutableArray *copiedArray = [array mutableCopy];
NSMutableArray *retainedArray = [array retain];

[retainedArray addObject:@"Retained Third"];
[copiedArray addObject:@"Copied Third"];

NSLog(@"array = %@",array);
NSLog(@"Retained Array = %@",retainedArray);
NSLog(@"Copied Array = %@",copiedArray);

array = (
    First,
    Second,
    "Retained Third"
)
Retained Array = (
    First,
    Second,
    "Retained Third"
)
Copied Array = (
    First,
    Second,
    "Copied Third"
)

A surrounding explanation would seriously improve your answer.
C
Chen Rui

assign assign is a default property attribute assign is a property attribute tells the compiler how to synthesize the property’s setter implementation copy: copy is required when the object is mutable copy returns an object which you must explicitly release (e.g., in dealloc) in non-garbage collected environments you need to release the object when finished with it because you are retaining the copy retain: specifies the new value should be sent “-retain” on assignment and the old value sent “-release” if you write retain it will auto work like strong Methods like “alloc” include an implicit “retain”