1

I have been learning and using Objective-C for quite some time now (it also kind of was my first OOP language) and I finally would like to know how to correctly name synthesized properties.

Let's take the following scenario:

I have got a property called someVariable.

@property (nonatomic, retain) NSString *someVariable;

and synthesize it

@synthesize someVariable;

How would the custom setter look like conventionally ?


1)

I would go ahead and say something like

-(void)setSomeVariable:(NSString *)someVar{

    //input parameter MAY sound/look foreign due to the difference to the property
    someVariable = someVar;

}

2) (illegal)

But I would like to name the formal parameter just like the property for the sake of readability and convenience. More like in Java like this:

-(void)setSomeVariable:(NSString *)someVariable{

    //obviously illegal because this would call the setter over and over again
    self.someVariable = someVariable; 

}

3) (unconventional)

and according to what I have been reading in the past this

@synthesize someVariable = _someVariable; 

is said to be unconventional and not supposed to be used.


So, am I correct in concluding that the way I have been doing it until now, is the only way to create a custom setter ?

the_critic
  • 12,720
  • 19
  • 67
  • 115

1 Answers1

3

3) is not unconventional, it's exactly what the compiler does if you don't provide the @synthesize statement.

This means that, without the @synthesize statement and the ivar declaration, you have an implicit ivar named _someVariable, and a custom setter would usually have a parameter named someVariable

-(void)setSomeVariable:(NSString *)someVariable {
    _someVariable = someVariable;
}

Also note that providing custom setter and getter methods for a particular property indicates to the Xcode compiler to not provide the implicit ivar (here _someVariable). In the case of readonly properties, the same if true if you provide just the getter method.

WWDC 2012 session 405 provides a lot of details around Objective-C constructs for modern versions of the compiler.

EDIT

As H2CO3 has suggested in his answer, the code I wrote assumes you're using ARC. If you are using MRC, the setter method would rather be :

-(void)setSomeVariable:(NSString *)someVariable {
    [someVariable retain];
    [_someVariable release];
    _someVariable = someVariable;
}
matehat
  • 5,214
  • 2
  • 29
  • 40
  • Yeah, I should have done my research. Thank you very much. – the_critic Jan 10 '14 at 20:58
  • hey, no question is a wrong question :) – matehat Jan 10 '14 at 21:01
  • @matehat was that you downvoting my answer? if so, explain why. Also, this is wrong or unsafe at best. Objects are assumed to be retained by setters throughout all sorts of Objective-C code which this code doesn't do. (Also, here it's more than an assumption: the property declaration says `(retain)`.) –  Jan 10 '14 at 22:13
  • I really didn't downvote your answer. I too am surprised. Your answer is more complete than mine! I only downvote misleading answers... – matehat Jan 10 '14 at 22:15
  • @matehat maybe yet someone who assumes that MRC doesn't exist (that's the complaint I keep receiving... whatever. I suggest you mention the memory management issue I mentioned anyway -- may save OP a couple of debugging hours.) –  Jan 10 '14 at 22:16