. 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]; } }
Rajkumar Gurunathan Dec 26 '13 at 17:18 2013-12-26 17:18
source share