Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of Synthesize

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. :)

like image 542
JoseSwagKid Avatar asked Feb 02 '13 02:02

JoseSwagKid


People also ask

What is the purpose of synthesis in research?

Synthesis is a form of analysis related to comparison and contrast, classification and division. On a basic level, synthesis involves bringing together two or more sources, looking for themes in each. In synthesis, you search for the links between various materials in order to make your point.

What is the purpose of synthesis in literature?

About SynthesisCombining elements of several sources to help you make a point. Describing how sources converse each other. Organizing similar ideas together so readers can understand how they overlap. Synthesis helps readers see where you add your own new ideas to existing knowledge.

What do we mean by synthesize?

1. formal. a : to make (something) by combining different things. She synthesized the treatment from traditional and modern philosophies of medicine.


2 Answers

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

like image 61
Firo Avatar answered Sep 26 '22 14:09

Firo


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.

like image 27
Jano Avatar answered Sep 25 '22 14:09

Jano