0

I am having some difficulty understanding what objects are supposed to be synthesized. For example:

@interface DoSomething : UIView

@property (strong, nonatomic) UIColor *frameColor;
@property BOOL toggleScrollability;
- (void) changeBackgroundColorOfView;

@end

In the .m file, which of these three items should be synthesized? Is there any disadvantage if I try and synthesize them all? In general, what is the rule of thumb for what objects you are supposed to synthesize?

David542
  • 104,438
  • 178
  • 489
  • 842

2 Answers2

3

The first two are properties; the third is an instance method. @synthesize applies only to properties.

However, if you're building for iOS 6 or newer, you don't need to synthesize at all. The compiler has handled this automatically for the last few years now.

rickster
  • 124,678
  • 26
  • 272
  • 326
  • So the autocompiler treats it as `@synthesize frameColor = _frameColor` ? – David542 Oct 04 '14 at 20:00
  • "You don't need to synthesize at all" ... Note, there are still situations where you must `@synthesize`. See the accepted answer of http://stackoverflow.com/questions/19784454/when-should-i-use-synthesize-explicitly for a clear articulation of these situations. But you're right that, in the vast majority of cases, we don't manually synthesize any more. – Rob Oct 05 '14 at 02:26
0

You can only synthesize properties, so you won't be able to write @synthesize changeBackgroundColorOfView.

Since XCode 4 I think, you don't have to use @synthesize anymore. The compiler automatically add it when it needs to be and you can access it by adding a _ before the name of your property.

In your example, you will access the frameColor property as _frameColor if you are in the DoSomething class, doSomethingInstance.frameColor if not.

But, you can always add it yourself if you want to rename your property for internal stuff.

See Apple's reference.

Imotep
  • 2,006
  • 2
  • 25
  • 38