Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@property and @synthesize

I'm very new to Objective C. (Two days now). When read about @synthesize, it seemed to overlap with my understanding @property (which I thought I understood) ... So, some details need to be ironed out in my mind ... it's bugging me.

Please correct me if I'm wrong about differences of @property and @synthesize:

If you declare a @property in your @interface, then you're telling the world that users can expect to use standard getters and setters for that property. Futhermore, XCode will make generic getters and setters for you. ... BUT, To what degree does that happen with the @property declaration? ( I.E. does that mean "completely" ... like unseen declarations for it in your @interface, and also unseen code in your @interface?

-Or-

Does @property take care of the unseen code declarations in your @interface only - whereas @synthesize takes care of the unseen code implementation in your @implementation section? )

like image 914
RichWalt Avatar asked Oct 04 '12 15:10

RichWalt


People also ask

What is @synthesize in Swift?

The @synthesize directive tells the compiler to generate the getter and setter accessor methods for us saving a lot of error prone typing. In this case I also chose to name the instance variable using the common naming convention of prefixing the property with an underscore.

What is @synthesize in Objective-C?

@synthesize tells the compiler to take care of the accessor methods creation i.e it will generate the methods based on property description. It will also generate an instance variable to be used which you can specify as above, as a convention it starts with _(underscore)+propertyName.

What is @property in Objective-C?

The goal of the @property directive is to configure how an object can be exposed. If you intend to use a variable inside the class and do not need to expose it to outside classes, then you do not need to define a property for it. Properties are basically the accessor methods.

What are the differences between implementing a @property with @dynamic or synthesize?

@synthesize will generate getter and setter methods for your property. @dynamic just tells the compiler that the getter and setter methods are implemented not by the class itself but somewhere else (like the superclass or will be provided at runtime).


2 Answers

First, note that the latest version of Xcode does not require @synthesize at all anymore. You can (and should) just omit it. That said, here's what the pieces do.

@property is a declaration of accessors. It is just a declaration. There is very little difference between the following:

@property (nonatomic, readwrite, strong) NSString *something;

vs.

- (NSString *)something;
- (void)setSomething:(NSString)aSomething;

The main difference is that declaring these methods using @property lets the compiler automatically generate (synthesize) the implementations for you. There is no requirement that you let the compiler do it for you. You are absolutely free to implement something and setSomething: by hand, and it is common to do. But, if you don't implement them by hand, the compiler will automatically create an ivar for you called _something and create a reasonable implementation for the getter and setter.

In older versions of Xcode, you had to explicitly request the auto-generation using the @synthesize keyword. But that is no longer required. Today, the only reason to use @synthesize is if you want the ivar to have a non-standard name (never do that).

A key point here is that the methods something and setSomething: are just methods. There is nothing magical about them. They're not special "property methods." They're just methods that by convention access a piece of state. That piece of state is often stored in an ivar, but does not need to be.

To be even more clear: object.something does not mean "return the ivar named _something from object." It means "return the result of [object something], whatever that does." It is common for that to return the value of an ivar.

You should declare all of your state (internal and external) using @property declarations, and you should avoid directly declaring ivars. You should also always access your properties via their accessors (self.something), except in the init and dealloc methods. In init and dealloc, you should directly use the ivar (_something).

like image 66
Rob Napier Avatar answered Nov 28 '22 10:11

Rob Napier


@property declares a property on your class with whatever atomicity and setter semantics you provide.

With Xcode 4.4, autosynthesis is available wherein you are provided with a backing ivar from your property without declaring it in @synthesize. This ivar has the form of _propertyName where your property name is propertyName.

like image 34
FluffulousChimp Avatar answered Nov 28 '22 08:11

FluffulousChimp