-3

Possible Duplicate:
What exactly does @synthesize do?
Can someone explain this @synthesize syntax?

@interface Duck : NSObject {

    NSArray *_feathers;

}
@property (nonatomic,retain) NSArray *feathers;

@end

@implementation Duck

@synthesize feathers=_feathers;

@end

I want to know what exactly is going on when you doing @synthesize feathers = _feathers ?

Community
  • 1
  • 1
Shinoj
  • 811
  • 2
  • 8
  • 18
  • 1
    you should look at this question http://stackoverflow.com/questions/822487/how-does-an-underscore-in-front-of-a-variable-in-a-cocoa-objective-c-class-work – Sakares Mar 21 '12 at 08:23
  • 1
    Start reading the [Objective-C](https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html) documentation. En especially the part about [properties](https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101-SW1) – rckoenes Mar 21 '12 at 08:24
  • Apart from Apple's doc some search could help you - even in this site. Just yesterday I answered the same question: http://stackoverflow.com/questions/9771434/iphone-instance-variable-issue/9771696#9771696 – MrTJ Mar 21 '12 at 08:34

1 Answers1

1

in your case (since your property is nonatomic)

@synthesize feathers=_feathers;

is equal to

- (void)setFeathers:(NSArray *)newFeathers 
{
    if (newFeathers != _feathers)
    { 
        [_feathers release];
        _feathers = [newFeathers retain];
    }
}

- (NSArray *)feathers
{
    return feathers_;
}
Slipp D. Thompson
  • 33,165
  • 3
  • 43
  • 43
dariaa
  • 6,285
  • 4
  • 42
  • 58
  • 2
    Not quite - you also have to check to see that `_fathers != fathers` in the setter incase you dealloc the fathers object by mistake with your first `release`. (Another way of dealing with the problem is to `autorelease` instead of `release`) – deanWombourne Mar 21 '12 at 08:36
  • 1
    @deanWombourne Or to retain the new value before releasing the old one, so that if it's the same object the retainCount won't dip down to 0. – Slipp D. Thompson Mar 11 '13 at 00:32
  • @SlippD.Thompson That's what I meant :) - your explanation is better though! – deanWombourne Mar 11 '13 at 19:29
  • @deanWombourne It seems to me to be two different approaches.  Yours (**checking that the two variables aren't the exact same object/value**) works better when setter has additional side-effects that one doesn't want to trigger, or when one is dealing with assigned values not object pointers.  The **retain-before-retain** approach comes in handy tricky ownership situations… when passing ownership, dealing with threads, and other times I can't remember now and generally try to avoid.  Regardless, it has its time and place _(usually the same time & place that one is scratching one's own head)_. – Slipp D. Thompson Mar 12 '13 at 05:40
  • Ah, I would have done your 'extra retain instead of if' method by `autorelease` instead of `release` on the initial object :) I guess it depends on how you want your memory management to work. – deanWombourne Mar 12 '13 at 10:30
  • That would work, but I try to avoid `autorelease` where unnecessary, especially in “client” code that we aren't sure of how often it'll be called.  A misplaced `autorelease` in a low-level/frequently-called function can quickly lead to a memory usage spike.  And ownership isn't as tricky in low-level code; `autorelease` has much more purpose in high-level systems. – Slipp D. Thompson Mar 20 '13 at 20:47
  • FWIW, the Clang documentation shows a pseudo implementation of what it synthesizes for the setter of `strong`/`retain` properties, and shows the retain-new-before-release-old approach: [clang.llvm.org/docs/AutomaticReferenceCounting.html#arc-runtime-objc-storestrong](http://clang.llvm.org/docs/AutomaticReferenceCounting.html#arc-runtime-objc-storestrong) – Slipp D. Thompson Mar 28 '13 at 15:33