-1

I'm just starting to use Objective-C and I need to clarify something

When I @synthesize a @property, it is common convention to do the following:

@interface Class : ParentClass

@property propertyName

@end

@implementation

@synthesize propertyName = _propertyName;

@end

I've seen plenty of questions and answers suggesting that "_propertyName" is widely accepted as the "correct" way to synthesize properties. However, does it serve ANY purpose? Or is it merely to increase readability and identify instance variables?

taskinoor
  • 45,586
  • 12
  • 116
  • 142
ngoue
  • 1,045
  • 1
  • 12
  • 25
  • 2
    Why dont you just try and search before asking.Lots of info already in forum – Lithu T.V Jul 03 '13 at 05:11
  • I did search. I found multiple similar questions, but they didn't seem to be asking the same thing… And if they were, I misunderstood them. – ngoue Jul 03 '13 at 05:27
  • I have put some links in the answer see that – Lithu T.V Jul 03 '13 at 05:29
  • Possible duplicate of: http://stackoverflow.com/a/5170703/1704346, http://stackoverflow.com/a/5170739/1704346, http://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW2, https://www.google.co.in/search?q=synthesize%20in%20objective%20c&oq=synthesize%20&aqs=chrome.2.57j0l3j62l2.7620j0&sourceid=chrome&ie=UTF-8, http://stackoverflow.com/questions/3802851/objective-c-synthesize-property-name-overriding (cc @LithuT.V) – CodaFi Jul 03 '13 at 05:31
  • This is a duplicate of [How does an underscore in front of a variable in an ObjC class work?](http://stackoverflow.com/q/822487) – jscs Jul 03 '13 at 07:34
  • And here's another 16: http://stackoverflow.com/q/5582448/ http://stackoverflow.com/q/6049269/ http://stackoverflow.com/q/2371489/ http://stackoverflow.com/q/7174277/ http://stackoverflow.com/q/5659156 http://stackoverflow.com/q/837559/ http://stackoverflow.com/q/6146244/ http://stackoverflow.com/q/10651535/ http://stackoverflow.com/q/6124109/ http://stackoverflow.com/q/8145373/ http://stackoverflow.com/q/3521254/ http://stackoverflow.com/q/6064283/ http://stackoverflow.com/q/9696359/ http://stackoverflow.com/q/5521499/ http://stackoverflow.com/q/5466496/ http://stackoverflow.com/q/2114587/ – jscs Jul 03 '13 at 07:35

4 Answers4

2

It makes it so that if you accidentally leave off "self." you get a nice compiler error instead of silently not having your methods called.

Catfish_Man
  • 41,261
  • 11
  • 67
  • 84
1

From http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html

You Can Customize Synthesized Instance Variable Names

As mentioned earlier, the default behavior for a writeable property is to use an instance variable called _propertyName.

If you wish to use a different name for the instance variable, you need to direct the compiler to synthesize the variable using the following syntax in your implementation:

@implementation YourClass @synthesize propertyName = instanceVariableName; ... @end

Also:

Note: The compiler will automatically synthesize an instance variable in all situations where it’s also synthesizing at least one accessor method. If you implement both a getter and a setter for a readwrite property, or a getter for a readonly property, the compiler will assume that you are taking control over the property implementation and won’t synthesize an instance variable automatically. If you still need an instance variable, you’ll need to request that one be synthesized: @synthesize property = _property;

shusson
  • 5,542
  • 34
  • 38
0

By doing this the generated accessor actually got to know which variable(iVar) to use.

rishi
  • 11,779
  • 4
  • 40
  • 59
0

Yea, It increases the readability & also separates the private & public variables to understand & use. Private variable of Class generally written in "propertyName" format.You can say it is a coding convention where Private Variable Names use '' as prefix and Public Variables or Property Names are lowerCamelCase.

Babul Mirdha
  • 3,816
  • 1
  • 22
  • 25