简体   繁体   English

使用未声明的标识符

[英]Use of undeclared identifier

I'm trying to use this project which is a synthesizer for Objective-C for an iPhone application I'm building. 我正在尝试使用这个项目 ,它是Objective-C的合成器,用于我正在构建的iPhone应用程序。 However, I'm having trouble with the MHAudioBufferPlayer class. 但是,我遇到了MHAudioBufferPlayer类的问题。

In the MHAudioBufferPlayer.m class , I'm getting a bunch of Use of undeclared identifier errors for _gain , _playing , and _audioFormat . MHAudioBufferPlayer.m类中 ,我收到了一堆Use of undeclared identifier _gain_playing_audioFormat Use of undeclared identifier错误。 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. 但是,它们在没有下划线的MHAudioBufferPlayer.h类中声明。

I'm sort of confused by this as I'm new to Objective-C. 因为我是Objective-C的新手,所以我对此感到困惑。 Does an underscore denote a special action to be taken? 下划线表示要采取特殊措施吗? Is it supposed to be translated into self.gain , self.playing , etc.? 它应该被翻译成self.gainself.playing等吗? 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;
}

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. 如果您使用的是Xcode4.4以后的新编译器,那么对于您的每个属性,它会创建一个自动合成,并使用_(下划线)作为前缀。

Like, if you have created @property.... playing; 就像,如果你创建了@property.... playing;

then the compiler creates @synthesize playing=_playing; 然后编译器创建@synthesize playing=_playing;

If you are in older versions of Xcode, this need to be done manually. 如果您使用的是旧版Xcode,则需要手动完成。

Depending on what version of XCode you are using, and compiler there are different ways of doing it. 根据您使用的XCode版本和编译器,有不同的方法。 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. 我不知道你对OOP有多熟悉,如果你不是我建议你读一下定制者和吸气剂和物品,因为它是你从现在开始做的几乎所有事情的基础。

Some examples, Old school style, will create an ivar. 一些例子,老派风格,将创造一个伊娃。 In your .h: 在你的.h:

@interface TheViewController : UIViewController{
    NSString *theString;
}

A bit new style, will create setter and getter In your .h. 一点点新风格,将在你的.h中创建setter和getter。

@interface TheViewController : UIViewController

@property (nonatomic, weak) NSString *theString;

In your .m file: 在.m文件中:

@implementation TheViewController

@synthesize theString = _theString;

Can be accessed by _theString or self.theString 可以通过_theString或self.theString访问

The new way of doing it. 这样做的新方式。 In your .h file: 在你的.h文件中:

@property (nonatomic, weak) NSString *theString;

The compiler Will create everything the above way did. 编译器将以上述方式创建所有内容。

Hope that helps you a bit. 希望对你有所帮助。

@synthesize generated by Xcode => 4.4 as the default. 由Xcode => 4.4生成的@synthesize作为默认值。 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. 如果您没有显式创建自己的@synthesize语句 ,则由Xcode为您创建的生成的私有实例变量ivar具有前导下划线' '。 You MUST include the leading ' ' when sending messages to this property (typically UI element as an outlet from your controller.m file). 在向此属性发送消息时, 您必须包含前导' '(通常是UI元素作为来自controller.m文件的插座)。

That is 那是

@property textField; @property textField;

[_textField setStringValue: @"foo"]; [_textField setStringValue:@“foo”]; if you DON'T write the '@synthesize'. 如果你不写'@synthesize'。

The compiler's done this for you, and has made a private instance variable by synthesizing the getter/setters. 编译器为您完成了这项工作,并通过合成getter / setter创建了一个私有实例变量。 The convention is to make the private ivar the name of the property prepended by the leading underscore. 惯例是使私有ivar成为前导下划线前面的属性名称。

OR 要么

@synthesize textField; @synthesize textField; @property textField; @property textField; [textField setStringValue: @"foo"]; [textField setStringValue:@“foo”]; if you DO write your own '@synthesize' or are < Xcode 4.4. 如果你自己编写'@synthesize'或者是<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 '_'. 在这里,编译器没有为你完成,你的ivar名称/属性名称是相同的,可以使用没有前导'_'。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM