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

5

34

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.

Levigate answered 20/2, 2012 at 21:28 Comment(4)
Do you have a link for where @synthesize is described as unnecessary?Astonishment
Perhaps he has seen some information about the currently-under-NDA Xcode 4.4 and has become confused about version numbers.Racy
@Astonishment added a link. Perhaps I'm misreading "@synthesize is automatic in the latest versions of the LLVM 2.0 compiler".Levigate
@robmayoff more like I've been reading potentially outdated information :-) See the links above.Levigate
G
25

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
Gloucestershire answered 1/8, 2012 at 21:3 Comment(0)
R
14

As of Xcode 4.4, if you don't write @synthesize or @dynamic for a property. the compiler acts as though you had written @synthesize property = _property.

Prior to Xcode 4.4, you must do one of the following things for each property or else the compiler will issue a warning and you will get a runtime error. In Xcode 4.4 or later, you may do any of the following things instead of letting the compiler automatically synthesize the property accessors and instance variable.

  1. Use the @synthesize directive.
  2. Use the @dynamic directive and somehow provide the property getter and (if necessary) setter at runtime.
  3. Explicitly write the property getter method and, if the property is readwrite, the property setter method.

Note that you can use the @synthesize directive (or the @dynamic directive) and also explicitly provide the getter and/or setter methods. But @synthesize provides them if you omit them.

Racy answered 20/2, 2012 at 21:34 Comment(1)
If you provide BOTH the getter and the setter, then you lose the automatic ivar, right?Vilify
C
13

From the New Features in Xcode 4.4 document:

Objective-C @properties are synthesized by default when not explicitly implemented.

So @synthesize is automatic by default starting from Xcode 4.4 with the LLVM 4.0 Compiler.

Carbonize answered 14/9, 2012 at 21:49 Comment(1)
Note that automatic synthesis is not supported for 32-bit Mac OS X.Snappy
S
13

Also, synthesize will not be automatic if you have implemented the setter AND getter manually. So if you wonder why you can't access _someVariable, having declared @property (...) SomeType someVariable, then it is because you have implemented the setSomeVariable: and someVariable methods.

Snowber answered 17/1, 2014 at 12:31 Comment(0)
A
5

You can turn off the synthesize warnings by clicking on the project name in the Project Navigator on the left then click All Cobined in Build Settings and then search for synthesize. That should be set to No.

Arabela answered 12/9, 2012 at 23:20 Comment(1)
Actually this is the only correct answer, an old answer. The rest goes to deep. Be sure to check the settings of all your targets, if you have multipleKandacekandahar

© 2022 - 2024 — McMap. All rights reserved.