• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Retain/Release and Properties clarification
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Retain/Release and Properties clarification
      • From: Eeyore <email@hidden>
References: 
 >Retain/Release and Properties clarification (From: John Tsombakos <email@hidden>)

  • Prev by Date: Retain/Release and Properties clarification
  • Next by Date: Re: Retain/Release and Properties clarification
  • Previous by thread: Retain/Release and Properties clarification
  • Next by thread: Re: Retain/Release and Properties clarification
  • Index(es):
    • Date
    • Thread