簡體   English   中英

使用未聲明的標識符

[英]Use of undeclared identifier

我正在嘗試使用這個項目 ,它是Objective-C的合成器,用於我正在構建的iPhone應用程序。 但是,我遇到了MHAudioBufferPlayer類的問題。

MHAudioBufferPlayer.m類中 ,我收到了一堆Use of undeclared identifier _gain_playing_audioFormat Use of undeclared identifier錯誤。 這是有道理的,因為這些標識符永遠不會在它們前面用下划線聲明。 但是,它們在沒有下划線的MHAudioBufferPlayer.h類中聲明。

因為我是Objective-C的新手,所以我對此感到困惑。 下划線表示要采取特殊措施嗎? 它應該被翻譯成self.gainself.playing等嗎? 我怎樣才能解決這個問題? 或者這個代碼只是錯誤嗎?

- (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;
}

如果您使用的是Xcode4.4以后的新編譯器,那么對於您的每個屬性,它會創建一個自動合成,並使用_(下划線)作為前綴。

就像,如果你創建了@property.... playing;

然后編譯器創建@synthesize playing=_playing;

如果您使用的是舊版Xcode,則需要手動完成。

根據您使用的XCode版本和編譯器,有不同的方法。 我不知道你對OOP有多熟悉,如果你不是我建議你讀一下定制者和吸氣劑和物品,因為它是你從現在開始做的幾乎所有事情的基礎。

一些例子,老派風格,將創造一個伊娃。 在你的.h:

@interface TheViewController : UIViewController{
    NSString *theString;
}

一點點新風格,將在你的.h中創建setter和getter。

@interface TheViewController : UIViewController

@property (nonatomic, weak) NSString *theString;

在.m文件中:

@implementation TheViewController

@synthesize theString = _theString;

可以通過_theString或self.theString訪問

這樣做的新方式。 在你的.h文件中:

@property (nonatomic, weak) NSString *theString;

編譯器將以上述方式創建所有內容。

希望對你有所幫助。

由Xcode => 4.4生成的@synthesize作為默認值。 如果您沒有顯式創建自己的@synthesize語句 ,則由Xcode為您創建的生成的私有實例變量ivar具有前導下划線' '。 在向此屬性發送消息時, 您必須包含前導' '(通常是UI元素作為來自controller.m文件的插座)。

那是

@property textField;

[_textField setStringValue:@“foo”]; 如果你不寫'@synthesize'。

編譯器為您完成了這項工作,並通過合成getter / setter創建了一個私有實例變量。 慣例是使私有ivar成為前導下划線前面的屬性名稱。

要么

@synthesize textField; @property textField; [textField setStringValue:@“foo”]; 如果你自己編寫'@synthesize'或者是<Xcode 4.4。

在這里,編譯器沒有為你完成,你的ivar名稱/屬性名稱是相同的,可以使用沒有前導'_'。

祝好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM