2012-09-02 44 views
0

我正在學習Objective C,我正在學習本書中的示例。但是在一些例子中,我不斷收到這個錯誤!我完全按照步驟操作,請幫忙!我已經嘗試將iOS模擬器恢復到其默認設置,但仍然沒有運氣。錯誤主題1:信號SIGBRT

這是一個可以錄製聲音並播放的應用程序。下面的代碼...

- (IBAction)recordAudio:(id)sender { 
    if ([self.recordButton.titleLabel.text isEqualToString:@"Record Audio"]){ 
     [self.audioRecorder record]; 
     [self.recordButton setTitle:@"Stop Recording" 
          forState:UIControlStateNormal]; 
    } else { 
     [self.audioRecorder stop]; 
     [self.recordButton setTitle:@"Record Audio" 
          forState:UIControlStateNormal]; 

     // Load the new sound in the audioplayer for playback 
     NSURL *soundFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() 
          stringsByAppendingPaths:@"sound.caf"]]; 

     self.audioPlayer = [[AVAudioPlayer alloc] 
          initWithContentsOfURL:soundFileURL error:nil]; 
    } 
} 
+2

請詳細描述發生了什麼事。它啓動應用程序時會崩潰嗎?當你點擊按鈕開始錄製?當你點擊按鈕停止錄製?其他時間? – Jim

+0

當你得到錯誤時,模擬器會在控制檯上打印出一些信息,如果你調出調試器,它會向你顯示異常堆棧。這會告訴你錯誤發生在哪裏。有了這些信息調試,這可能會很容易。沒有它,調試只是一個猜測遊戲。 –

回答

1

我跑你的代碼的一部分,這是錯誤消息:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** 
-[__NSCFString stringsByAppendingPaths:]: paths argument is not an array' 

由於文件說,stringsByAppendingPaths返回由單獨提出附加一個NSString的NSArray的對象每個NSString在一個NSArray路徑中指向接收者。 @「sound.caf」是一個NSString,而不是NSArray,它引發了異常。

更改如下:

NSURL *soundFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() 
         stringsByAppendingPaths:@"sound.caf"]]; 

要:

NSURL *soundFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() 
         stringByAppendingPathComponent:@"sound.caf"]]; 

,它應該工作。