Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom setter for copy property in objective-c

Should I have to manually copy object in custom setter with copy property in objective-c? For example,

I have a property:

@property (copy, nonatomic) NSString *someString;

and custom setter:

- (void)setSomeString:(NSString *)someString
{
  //option 1
  _someString = someString;
  //option 2
  _someString = [someString copy];
  //do other stuff
}

Is it option 1 enough for custom setter or I have to use option 2 instead in order to have copied object?

like image 327
razor28 Avatar asked Apr 16 '14 07:04

razor28


1 Answers

You can do whatever you like but you should use the second option. This is because it will be something like code documentation if other developer see it he or she will know that you copy the string just by looking at:

@property (copy, nonatomic) NSString *someString;

The same if you use retain/assign it's best practice to retain/assign object in custom setter. It will make your code more clearer more documented and much more understandable for others developers.

like image 85
Greg Avatar answered Sep 30 '22 17:09

Greg