Question

I'm trying to use this project which is a synthesizer for Objective-C for an iPhone application I'm building. However, I'm having trouble with the MHAudioBufferPlayer class.

In the MHAudioBufferPlayer.m class, I'm getting a bunch of Use of undeclared identifier errors for _gain, _playing, and _audioFormat. This makes sense, as those identifiers are never declared with an underscore in front of them. However, they are declared in the MHAudioBufferPlayer.h class without the underscores.

I'm sort of confused by this as I'm new to Objective-C. Does an underscore denote a special action to be taken? Is it supposed to be translated into self.gain, self.playing, etc.? How can I fix this? Or is this code just buggy?

- (id)initWithSampleRate:(Float64)sampleRate channels:(UInt32)channels bitsPerChannel:(UInt32)bitsPerChannel packetsPerBuffer:(UInt32)packetsPerBuffer
{
    if ((self = [super init]))
    {
        _playing = NO;
        _playQueue = NULL;
        _gain = 1.0;

        _audioFormat.mFormatID         = kAudioFormatLinearPCM;
        _audioFormat.mSampleRate       = sampleRate;
        _audioFormat.mChannelsPerFrame = channels;
        _audioFormat.mBitsPerChannel   = bitsPerChannel;
        _audioFormat.mFramesPerPacket  = 1;  // uncompressed audio
        _audioFormat.mBytesPerFrame    = _audioFormat.mChannelsPerFrame * _audioFormat.mBitsPerChannel/8;
        _audioFormat.mBytesPerPacket   = _audioFormat.mBytesPerFrame * _audioFormat.mFramesPerPacket;
        _audioFormat.mFormatFlags      = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;

        _packetsPerBuffer = packetsPerBuffer;
        _bytesPerBuffer = _packetsPerBuffer * _audioFormat.mBytesPerPacket;

        [self setUpAudio];
    }
    return self;
}
Was it helpful?

Solution

If you are using new compiler that comes with Xcode4.4 onwards, then for each of your property it creates an automatic synthesize with _(underscore) as prefix.

Like, if you have created @property.... playing;

then the compiler creates @synthesize playing=_playing;

If you are in older versions of Xcode, this need to be done manually.

OTHER TIPS

@synthesize generated by Xcode => 4.4 as the default. The generated private instance variable, ivar, created for you by Xcode has a leading underscore '' if you don't explicitly create your own @synthesize statement. You MUST include the leading '' when sending messages to this property (typically UI element as an outlet from your controller.m file).

That is

@property textField;

[_textField setStringValue: @"foo"]; if you DON'T write the '@synthesize'.

The compiler's done this for you, and has made a private instance variable by synthesizing the getter/setters. The convention is to make the private ivar the name of the property prepended by the leading underscore.

OR

@synthesize textField; @property textField; [textField setStringValue: @"foo"]; if you DO write your own '@synthesize' or are < Xcode 4.4.

Here, the complier has NOT done it for you, your ivar name/property name are the same and can be used w/o the leading '_'.

Good luck.

Depending on what version of XCode you are using, and compiler there are different ways of doing it. I don't know how familiar you are with OOP, if you are not I suggest you read up a bit on setters and getters and objects as it is the basis of almost everything you will do from now on.

Some examples, Old school style, will create an ivar. In your .h:

@interface TheViewController : UIViewController{
    NSString *theString;
}

A bit new style, will create setter and getter In your .h.

@interface TheViewController : UIViewController

@property (nonatomic, weak) NSString *theString;

In your .m file:

@implementation TheViewController

@synthesize theString = _theString;

Can be accessed by _theString or self.theString

The new way of doing it. In your .h file:

@property (nonatomic, weak) NSString *theString;

The compiler Will create everything the above way did.

Hope that helps you a bit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top