Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use @synthesize explicitly?

As far as I know, since XCode 4.4 the @synthesize will auto-generate the property accessors. But just now I have read a sample of code about NSUndoManager, and in the code it noticed that the @synthesize is added explicitly. Like:

@interface RootViewController  ()  @property (nonatomic, strong) NSDateFormatter *dateFormatter; @property (nonatomic, strong) NSUndoManager *undoManager;  @end  @implementation RootViewController //Must explicitly synthesize this @synthesize undoManager; 

I am feeling puzzled now... When should I add @synthesize explicitly to my code?

like image 662
罗泽轩 Avatar asked Nov 05 '13 08:11

罗泽轩


People also ask

What is @synthesize in IOS?

@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.

Which is the default for synthesized properties?

Default Synthesis Of Properties Clang provides support for autosynthesis of declared properties. Using this feature, clang provides default synthesis of those properties not declared @dynamic and not having user provided backing getter and setter methods.

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 Ivar Objective C?

Accessing an ivar through a getter/setting involves an Objective-C method call, which is much slower (at least 3-4 times) than a "normal" C function call and even a normal C function call would already be multiple times slower than accessing a struct member.


1 Answers

There's a lot of answers, but also a big confusion. I'll try to put some order (or increase the mess, we'll see...)

  1. Let's stop talking about Xcode. Xcode is an IDE. clang is a compiler. This feature we are discussing is called autosynthesis of properties and it's an Objective-C language extension supported by clang, which is the default compiler used by Xcode.
    Just to make it clear, if you switch to gcc in Xcode, you won't benefit from this feature (regardless from the Xcode version.) In the same way if you use a text editor and compile using clang from the command line, you will.

  2. Thank to autosynthesis you don't need to explicitly synthesize the property as it will be automatically synthesized by the compiler as

    @synthesize propertyName = _propertyName 

    However, a few exceptions exist:

    • readwrite property with custom getter and setter

      when providing both a getter and setter custom implementation, the property won't be automatically synthesized

    • readonly property with custom getter

      when providing a custom getter implementation for a readonly property, this won't be automatically synthesized

    • @dynamic

      when using @dynamic propertyName, the property won't be automatically synthesized (pretty obvious, since @dynamic and @synthesize are mutually exclusive)

    • properties declared in a @protocol

      when conforming to a protocol, any property the protocol defines won't be automatically synthesized

    • properties declared in a category

      this is a case in which the @synthesize directive is not automatically inserted by the compiler, but this properties cannot be manually synthesized either. While categories can declare properties, they cannot be synthesized at all, since categories cannot create ivars. For the sake of completeness, I'll add that's it's still possible to fake the property synthesis using the Objective-C runtime.

    • overridden properties (new since clang-600.0.51, shipping with Xcode 6, thanks Marc Schlüpmann)

      when you override a property of a superclass, you must explicitly synthesize it

It's worth noting that synthesizing a property automatically synthesize the backing ivar, so if the property synthesis is missing, the ivar will be missing too, unless explicitly declared.

Except for the last three cases, the general philosophy is that whenever you manually specify all the information about a property (by implementing all the accessor methods or using @dynamic) the compiler will assume you want full control over the property and it will disable the autosynthesis on it.

Apart from the cases that are listed above, the only other use of an explicit @synthesize would be to specify a different ivar name. However conventions are important, so my advice is to always use the default naming.

like image 109
Gabriele Petronella Avatar answered Oct 04 '22 15:10

Gabriele Petronella