Goal C - Assign, Copy, Save

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

  • Can someone explain the difference between assignment, copying and saving with some analogy?
  • How do you handle a function that returns a pointer variable, and how do you exchange messages with a return pointer?
+72
memory-management ios objective-c attributes macos
Dec 22 '10 at 15:51
source share
7 answers

Updated answer for modified documentation

The information now extends to several manuals in the documentation. Here is a list of required values:

Now the answer to this question depends entirely on whether you are using an application controlled by ARC (the modern default value for new projects) or forced manual memory management.

Appoint against the weak . Use assign to indicate the property pointer to the address of the object, without saving it or otherwise oversee it; use weak so that the property automatically points to zero if the object assigned to it is freed. In most cases, you will want to use weak so as not to try to access the freed object (illegal access to the memory address is " EXC_BAD_ACCESS ") if you do not perform the correct cleaning.

Save and copy . Declared properties use save by default (so you can simply omit it completely) and automatically control the counter of references to objects if another object is assigned to the property or it is set to zero; Use copy to automatically send the newly assigned object a -copy message (which will create a copy of the transferred object and instead assign this copy to the property - useful (even required) in some situations where the assigned object can be modified after it is set as a property of some other object (which would mean that the modification / mutation is applicable to this property as well).

+112
Dec 22 '10 at 16:00
source share

The memory programming guide from the iOS help library has the basics of assigning, copying, and saving with analogs and examples.

copy Makes a copy of the object and returns it, keeping the number at 1. If you copy the object, you have a copy. This applies to any method that contains a copy of the word, where "copy" refers to the returned object.

save Increases the number of object holdings by 1. Participates in the object.

release Decreases the number of stored objects by 1. Cancels ownership of the object.

+29
Dec 22 '10 at 15:59
source share

Copy vs Assign vs Save

Purpose - these are primitive values โ€‹โ€‹of type BOOL, NSInteger or double. For objects, use saving or copying, depending on whether you want to save a link to the original object or make a copy of it.

hidden function

 NSString * string ; 

When you call this expression

 string = @"Your name"; 

the method will be automatically called

 - (void)setString:(NSString*)newString { } 
  • Assign

In your setter method for a property, there is simply assigning your instance variable to a new value,

 - (void)setString:(NSString*)newString { string = newString; } 

This can cause problems because Objective-C objects use reference counting and, therefore, without storing the object, there is a chance that the line might be freed while you are still using it.

  • Save

this saves the new value in the setter method. For example: itโ€™s safer because you expressly declare that you want to keep a reference to the object, and you must free it before it is redistributed.

 - (void)setString:(NSString*)newString { [newString retain]; [string release]; string = newString; } 
  • Copy

this makes a copy of the string in the setter method: this is often used with strings, since copying the original object ensures that it does not change while you use it.

  - (void)setString:(NSString*)newString { if(string!=newString){ [string release]; string = [newString copy]; } } 
+16
Nov 17 '15 at 8:15
source share

. Purpose - for primitive values โ€‹โ€‹such as BOOL, NSInteger or double. To use objects, save or copy, depending on whether you want to save a link to the source object or make a copy of it. ยท Assign: In your setter method for a property, there is a simple assignment of an instance variable for a new value, for example:

 -(void)setString:(NSString*)newString{ string = newString; } 

This can cause problems, since Objective-C objects use reference counting, and therefore, without storing the object, there is a chance that the string might be freed while you are still using it. ยท Save: this saves the new value in the setter method. For example: This is safer because you explicitly declare that you want to keep a reference to the object, and you must release it before it is released.

 - (void)setString:(NSString*)newString{ [newString retain]; [string release]; string = newString; } 

ยท Copy: this makes a copy of the string in the setter method: This is often used with strings, since copying the original object ensures that it will not be changed while you use it.

 - (void)setString:(NSString*)newString{ if(string!=newString){ [string release]; string = [newString copy]; } } 
+4
Dec 26 '13 at 17:18
source share
  • to appoint

    • assign - default property attribute
    • assign is a property attribute that tells the compiler how to synthesize an implementation of a property installer
  • Copy:

    • copying is required when the object is modified.
    • copy returns an object that you must explicitly free (e.g. in dealloc) in non-garbage environments
    • you need to free the object when you're done with it because you are saving a copy
  • save

    • indicates that the new value should be sent "-receive" when assigned and the old value "-release" sent
    • If you write, save, it will work as strong
    • Methods such as "alloc" include implicit "persistence"
+2
Apr 26 '15 at 7:01
source share
 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" ) 
+1
Nov 25 '16 at 19:33
source share

save vs. copy vs. assign

" assign " by default. In a setter created using @synthesize, the value will simply be assigned to the attribute. I understand that for attributes without a pointer, "assign" should be used.

" save " is necessary when the attribute is a pointer to an object. The setter generated by @synthesize will save (aka add a hold counter) the object. You will need to free the object when you are done with it.

" copy " is necessary when the object is modified. Use this if you need the value of the object as it is at the moment, and you do not want this value to reflect any changes made by other owners of the object. You will need to free the object when you finish it, because you are saving a copy.

-one
Mar 27 '17 at 11:30
source share



All Articles