2009-07-30 13 views
0

爲什麼不是這個代碼工作?該應用程序加載罰款,但是每當我按下按鈕應用程序鎖定(不會崩潰),並將我返回給Xcode,並有一個紅色箭頭指向其中一個行(表示在下面的行)。這個數組有什麼問題?每當我運行它鎖定的動作

我還得到三個警告(不是錯誤),一個說'NSMutableArray'可能不會響應'-objectAt:',第二個說未使用的變量'soundToPlay'第三個說隱式聲明函數'AudioServicesPlaySystemSound'應該我關心那些?

//In MainViewController.h 

    @interface MainViewController : UIViewController { 
     IBOutlet UIButton *Button;  
    } 

    @property (nonatomic, retain) UIButton *Button; 

    - (IBAction)playSound; 

    NSInteger currentSound; 
    NSMutableArray *soundArray; 

    @end 




    //In MainViewController.m 

     - (void)viewDidLoad { 
     currentSound = 0; 
     NSMutableArray *sounds = [[NSMutableArray alloc] init]; 
     [sounds addObject: @"0.wav"]; 
     [sounds addObject: @"1.wav"]; 
     [sounds addObject: @"2.wav"]; 
     [sounds addObject: @"3.wav"]; 
     [sounds addObject: @"4.wav"]; 
     [sounds addObject: @"5.wav"]; 
     [sounds addObject: @"6.wav"]; 
     [sounds addObject: @"7.wav"]; 
     [sounds addObject: @"8.wav"]; 
     [sounds addObject: @"9.wav"]; 
     [sounds addObject: @"10.wav"]; 
     [sounds addObject: @"11.wav"]; 
     [sounds addObject: @"12.wav"]; 
     [sounds addObject: @"13.wav"]; 
     [super viewDidLoad]; 
    } 

    - (IBAction)playSound { 
     NSString *soundToPlay = [soundArray objectAt: currentSound]; 
//First two errors here. 
     currentSound = (currentSound + 1) % [soundArray count]; //Red arrow points to this line. 
     AudioServicesPlaySystemSound (currentSound); 
//Third error here. 
    } 

回答

2

第一招: 的方法名沒有按不存在。你的意思是寫objectAtIndex而不是objectAt。 當它表示對象現在可能響應'-something'時,這是因爲方法未在頭文件中聲明。這會導致編譯器不確定該方法是否存在於實現文件中。當你使用內置類(NSArray,NSView等)時,這些警告通常意味着你拼錯了方法名。唯一要做的就是在文檔中查找並重新檢查。這種類型錯誤會導致應用程序崩潰,當嘗試將消息發送給沒有響應選擇器的對象時。

第二個: 這只是一個警告,告訴你一個變量沒有被使用。刪除線。正如你可以看到soundToPlay沒有被用在playSound方法中,它只是被聲明的。 如果你使用它的FX。與NSLog函數調用或在方法中,此警告應該消失。

第三個: 您可能會使用錯誤的函數。不能從你貼的代碼說。

其他: 丹尼爾是正確的。您的viewDidLoad方法正在創建一個本地數組,並且絕不會將其存儲在soundArray實例變量中。爲此,請在方法的底部添加一個

soundArray = sounds 

。或者直接寫聲音數組而不是聲音。

1

從看到的代碼,不似乎你曾經intialize soundArray,你是在填充一個viewDidLoad中的局部變量叫的聲音,從來沒有設置soundArray ...

+0

它看起來像.m文件中的Matlab代碼。在matlab中,你不需要初始化變量。 – 2009-07-30 18:40:31

+0

是什麼? soundArray沒有任何內容,通過上面的代碼即 – Daniel 2009-07-30 18:45:02