Re: Retain/Release and Properties clarification
Re: Retain/Release and Properties clarification
- Subject: Re: Retain/Release and Properties clarification
- From: Steve Sisak <email@hidden>
- Date: Mon, 03 Oct 2011 10:29:28 -0400
At 10:14 AM -0400 10/3/11, John Tsombakos wrote:
@interface AudioPlayerViewController : UIViewController {
AVAudioPlayer *audioPlayer;
}
@property (retain) AVAudioPlayer *audioPlayer;
In the .m file:
@synthesize audioPlayer;
in viewDidLoad:
audioPlayer = [self getSoundFile:"soundfile.wav"];
You got close with:
(side note... should that be self.audioPlayer = ... ? I keep getting
confused with accessors vs. ivars too ;) )
You do, indeed want:
self.audioPlayer = [self getSoundFile:"soundfile.wav"];
or
[self setAudioPlayer = [self getSoundFile:"soundfile.wav"]];
Without the self. you're bypassing the setter (which is responsible
for retain/release) and just setting the instance variable directly.
Therefore there's no retain.
This is one reason it's useful to use a different name for the
instance variable than the property -- say _audioPlayer with
@synthesize audioPlayer = _audioPlayer;
In this case,
audioPlayer = [self getSoundFile:"soundfile.wav"];
now uses the setter and
_audioPlayer = [self getSoundFile:"soundfile.wav"];
accesses the instance variable directly (no retain), (hopefully)
making the code more explicit preventing errors as you have to be
explicit to access the instance variable directly.
HTH,
-Steve
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden