28

I am using an iOS5 book to learn iOS programming.

@synthesize coolWord;

^synthesize is used for all properties in .m files

I heard that in iOS6 there is no need for synthesize, since it is automatically done for you. Is this true?

Does synthesize play any role for iOS6?

Thanks for the clarification. :)

Firo
  • 15,448
  • 3
  • 54
  • 74
JoseSwagKid
  • 389
  • 1
  • 6
  • 12
  • 1
    This is not a property of iOS 6. It is a property of Xcode 4.4. Take a look at the [Objective-C Feature Availability Index](https://developer.apple.com/library/ios/#releasenotes/ObjectiveC/ObjCAvailabilityIndex/index.html). This is the “Default synthesis of `@property` instance variables and accessor methods” feature. – rob mayoff Feb 02 '13 at 03:46
  • Even though it's a just a property of Xcode, I assume you can still do this for iOS6 apps though, right? – JoseSwagKid Feb 02 '13 at 03:55

6 Answers6

35

@synthesize in objective-c just implements property setters and getters:

- (void)setCoolWord:(NSString *)coolWord {
     _coolWord = coolWord;
}

- (NSString *)coolWord {
    return _coolWord;
}

It is true with Xcode 4 that this is implemented for you (iOS6 requires Xcode 4). Technically it implements @synthesize coolWord = _coolWord (_coolWord is the instance variable and coolWord is the property).

To access these properties use self.coolWord both for setting self.coolWord = @"YEAH!"; and getting NSLog(@"%@", self.coolWord);

Also note, both the setter and getter can still be manually implemented. If you implement BOTH the setter and getter though you NEED to also manually include @synthesize coolWord = _coolWord; (no idea why this is).

Firo
  • 15,448
  • 3
  • 54
  • 74
  • Thank you! You explained the synthesize concept and its role in iOS6 very well. :) – JoseSwagKid Feb 02 '13 at 03:20
  • 1
    The code that is actually generated is a bit more complicated and depends on how the property is defined and if you have implemented some or all of the generated methods yourself. For example if you have a atomic property (which is the default) then the generated setter will make sure that only one thread can modify the underlying variable at any time. There are dozens of other factors that influence the code that is generated. The fact that you don't have to worry about those things anymore (in most cases) is nice. ;) – Christian Kienle Feb 02 '13 at 23:48
  • 1
    Still can not understand synthesize, @Christian Kienle. – Arpit B Parekh Jan 19 '17 at 05:42
  • If I have one variable testString inside interfaqce {} block. And same property testString. I have to write synthesize. To tell the OS that both this variable are same. Why ? still I do not understand what is _ funda. – Arpit B Parekh Jan 19 '17 at 06:59
9

Autosynthesis in iOS6 still requires @synthesize

  • to generate accessor methods for properties defined in a @protocol.
  • to generate a backing variable when you included your own accessors.

The second case can be verified like this:

#import <Foundation/Foundation.h>
@interface User : NSObject
@property (nonatomic, assign) NSInteger edad;
@end
@implementation User
@end

Type: clang -rewrite-objc main.m and check that the variable is generated. Now add accessors:

@implementation User
-(void)setEdad:(NSInteger)nuevaEdad {}
-(NSInteger)edad { return 0;}
@end

Type: clang -rewrite-objc main.m and check that the variable is NOT generated. So in order to use the backing variable from the accessors, you need to include the @synthesize.

It may be related to this:

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.

Jano
  • 62,815
  • 21
  • 164
  • 192
  • Thats not true. You can also generate the ivar yourself: `@implementation MyClass { NSInteger _edad; } /* getter & setter here */ @end` – Christian Kienle Feb 02 '13 at 23:51
  • I was referring to autosynthesis. I edited the answer for clarification. – Jano Feb 03 '13 at 16:56
  • @Jana "to generate accessor methods for properties defined in a @protocol". Protocol is just a contract, how does the compiler know what setter and getter to generate? By looking at the contract ? – onmyway133 May 29 '14 at 08:44
  • Using `clang -rewrite-objc main.m` just gives me an enormous main.cpp file. Is there something I need to include to make it output Objective-C? – nevan king Aug 07 '18 at 15:00
4

I'm not sure how @synthesize relates to iOS6 but since Xcode 4.0, it's essentially been deprecated. Basically, you don't need it! Just use the @property declaration and behind the scenes, the compiler generates it for you.

Here's an example:

@property (strong, nonatomic) NSString *name;

/*Code generated in background, doesn't actually appear in your application*/
@synthesize name = _name;

- (NSString*)name
{
    return _name;
}

- (void) setName:(NSString*)name
{
    _name = name;
}

All that code is taken care of the complier for you. So if you have an applications that have @synthesize, it's time to do some cleanup.

You can view my similar question here which might help to clarify.

Community
  • 1
  • 1
barndog
  • 6,975
  • 8
  • 53
  • 105
2

I believe that @synthesize directives are automatically inserted in the latest Obj-C compiler (the one that comes with iOS 6).

The point of @synthesize pre-iOS 6 is to automatically create getters & setters for instance variables so that [classInstance getCoolWord] and [classInstance setCoolWord:(NSString *)aCoolWord] are generated. Because they are declared with @property, you also get the convenience of dot syntax for the getter and setter.

aakash
  • 1,001
  • 1
  • 8
  • 10
1
hope this will help little more

yes previously we have to synthesis the property by using @synthesis now it done by IDE itself.

but we can use it like

// what IDE does internally

@synthesis name=_name;

we use _name to access particular property but now you want synthesis by some other way like firstname you can do it like

@synthesis name= firstname

or just by name

@synthesis name=name
Anurag Bhakuni
  • 2,379
  • 26
  • 32
0

With automatic synthesis in iOS6, it is no longer necessary to specifically declare backing ivars or write the @synthesize statement. When the compiler finds a @property statement, it will do both on our behalf using the guidelines we’ve just reviewed. So all we need to do is declare a property like this:

@property (nonatomic, strong) NSString *abc;  

and in iOS 6, @synthesize abc = _abc, will be added automatically at compile time.

Dhruv Goel
  • 2,715
  • 1
  • 17
  • 17