Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under what conditions is @synthesize automatic in Objective-c?

Under what conditions is @synthesize automatic in Objective-c?

Perhaps when using LLVM 3.0 and up? From reading around the net it seems like @synthesize is unnecessary starting with Xcode 4. However I'm using Xcode 4 and receiving warnings when I don't @synthesize a property.

Some of the responses to Why don't properties get automatically synthesized seem to imply @synthesize can be omitted at some point under some circumstances.

Another (old) reference hinting that @synthesize might be automatic at some point in the future.

like image 549
SundayMonday Avatar asked Feb 20 '12 21:02

SundayMonday


People also ask

What does @synthesize do 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 does the compiler directive @synthesize do?

@synthesize mapView = mapView1; This line tells the compiler to create a setter and getter for mapView , and that they should use the ivar called mapView1 . Without the = mapView1 part, the compiler would assume that the property and ivar have the same name.

What is @dynamic in Objective C?

@objc means you want your Swift code (class, method, property, etc.) to be visible from Objective-C. dynamic means you want to use Objective-C dynamic dispatch.

What is a property in Objective C?

Objective-C properties offer a way to define the information that a class is intended to encapsulate. As you saw in Properties Control Access to an Object's Values, property declarations are included in the interface for a class, like this: @interface XYZPerson : NSObject.


1 Answers

As of clang 3.2 (circa February 2012), "default synthesis" (or "auto property synthesis") of Objective-C properties is provided by default. It's essentially as described in the blog post you originally read: http://www.mcubedsw.com/blog/index.php/site/comments/new_objective-c_features/ (except that that post describes the feature as "enabled, then disabled"; I don't know if that's an issue with Xcode or if the clang developers themselves have gone back and forth on the question).

As far as I know, the only case in which properties will not be default-synthesized in clang 3.2 is when those properties have been inherited from a protocol. Here's an example:

#import <Foundation/Foundation.h>  @protocol P @property int finicky; @end  @interface A : NSObject <P> @property int easygoing; @end  @implementation A @end  int main() { A *a = [A new]; a.easygoing = 0; a.finicky = 1; } 

If you compile this example, you'll get a warning:

test.m:11:17: warning: auto property synthesis will not synthesize property       declared in a protocol [-Wobjc-protocol-property-synthesis] @implementation A                 ^ test.m:4:15: note: property declared here @property int finicky;               ^ 1 warning generated. 

and if you run it, you'll get an error from the runtime:

objc[45820]: A: Does not recognize selector forward:: (while forwarding setFinicky:) Illegal instruction: 4 
like image 119
Quuxplusone Avatar answered Sep 30 '22 21:09

Quuxplusone