質問

iPhoneのこの小さなプロジェクトを構築することを魅力的ですが、IOSシミュレータに展開すると、「Libc ++ abi.dylib:NSException型の不明瞭な例外で終了する」というエラーが発生します。

コードはかなり簡単です、ここで行く:

whgviewController.h:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface WHGViewController : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate>

@property (strong, nonatomic) AVAudioRecorder* rec;
@property (strong, nonatomic) AVAudioPlayer* ply;
@property (strong, nonatomic) IBOutlet UIButton *recBtn;
@property (strong, nonatomic) IBOutlet UIButton *plyBtn;
@property (strong, nonatomic) IBOutlet UIButton *stopBtn;

- (IBAction)recordAudio:(id)sender;
- (IBAction)playAudio:(id)sender;
- (IBAction)stop:(id)sender;

@end
.

WhgViewController.m:

#import "WHGViewController.h"

@interface WHGViewController ()

@end

@implementation WHGViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _plyBtn.enabled = NO;
    _stopBtn.enabled = NO;

    NSArray *dirPaths;
    NSString *docsDir;

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    docsDir = dirPaths[0];

    NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"test.caf"];

    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

    NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                    AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt:16],
                                    AVEncoderBitRateKey,
                                    [NSNumber numberWithInt:2],
                                    AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:44100.0],
                                    AVSampleRateKey,
                                    nil];

    NSError *error = nil;
    _rec = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recordSettings error:&error];

    if(error){
        NSLog(@"error: %@", [error localizedDescription]);
    } else {
        [_rec prepareToRecord];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)recordAudio:(id)sender {
    _plyBtn.enabled = NO;
    _stopBtn.enabled = YES;
    [_rec record];
}

- (IBAction)playAudio:(id)sender {
    if(!_rec.recording){
        _recBtn.enabled = NO;
        _stopBtn.enabled = YES;

        NSError *error;

        _ply = [[AVAudioPlayer alloc] initWithContentsOfURL:_rec.url error:&error];

        _ply.delegate = self;

        if(error){
            NSLog(@"error: %@", [error localizedDescription]);
        } else {
            [_ply play];
        }
    }
}

- (IBAction)stop:(id)sender {

    _stopBtn.enabled = NO;
    _recBtn.enabled = YES;
    _plyBtn.enabled = YES;

    if(_rec.recording) {
        [_rec stop];
    } else if(_ply.playing) {
        [_ply stop];
    }
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    _recBtn.enabled = YES;
}

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error {
    NSLog(@"Decode error did occur");
}

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag {

}

- (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error {
    NSLog(@"Encode error did occur");
}

@end
.

これが完全なクラッシュレポートです:

2013-11-15 23:51:08.814 WhisperClient[51402:70b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<WHGViewController 0x9564950> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key playAudio.'
*** First throw call stack:
(
    0   CoreFoundation                      0x019665e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x016e98b6 objc_exception_throw + 44
    2   CoreFoundation                      0x019f66a1 -[NSException raise] + 17
    3   Foundation                          0x013aa9ee -[NSObject(NSKeyValueCoding) setValue:forUndefinedKey:] + 282
    4   Foundation                          0x01316cfb _NSSetUsingKeyValueSetter + 88
    5   Foundation                          0x01316253 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 267
    6   Foundation                          0x0137870a -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 412
    7   UIKit                               0x006f9a15 -[UIRuntimeOutletConnection connect] + 106
    8   libobjc.A.dylib                     0x016fb7d2 -[NSObject performSelector:] + 62
    9   CoreFoundation                      0x01961b6a -[NSArray makeObjectsPerformSelector:] + 314
    10  UIKit                               0x006f856e -[UINib instantiateWithOwner:options:] + 1417
    11  UIKit                               0x0056a605 -[UIViewController _loadViewFromNibNamed:bundle:] + 280
    12  UIKit                               0x0056adad -[UIViewController loadView] + 302
    13  UIKit                               0x0056b0ae -[UIViewController loadViewIfRequired] + 78
    14  UIKit                               0x0056b5b4 -[UIViewController view] + 35
    15  UIKit                               0x004939fd -[UIWindow addRootViewControllerViewIfPossible] + 66
    16  UIKit                               0x00493d97 -[UIWindow _setHidden:forced:] + 312
    17  UIKit                               0x0049402d -[UIWindow _orderFrontWithoutMakingKey] + 49
    18  UIKit                               0x0049e89a -[UIWindow makeKeyAndVisible] + 65
    19  UIKit                               0x00451cd0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
    20  UIKit                               0x004563a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    21  UIKit                               0x0046a87c -[UIApplication handleEvent:withNewEvent:] + 3447
    22  UIKit                               0x0046ade9 -[UIApplication sendEvent:] + 85
    23  UIKit                               0x00458025 _UIApplicationHandleEvent + 736
    24  GraphicsServices                    0x02f192f6 _PurpleEventCallback + 776
    25  GraphicsServices                    0x02f18e01 PurpleEventCallback + 46
    26  CoreFoundation                      0x018e1d65 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    27  CoreFoundation                      0x018e1a9b __CFRunLoopDoSource1 + 523
    28  CoreFoundation                      0x0190c77c __CFRunLoopRun + 2156
    29  CoreFoundation                      0x0190bac3 CFRunLoopRunSpecific + 467
    30  CoreFoundation                      0x0190b8db CFRunLoopRunInMode + 123
    31  UIKit                               0x00455add -[UIApplication _run] + 840
    32  UIKit                               0x00457d3b UIApplicationMain + 1225
    33  WhisperClient                       0x0000363d main + 141
    34  libdyld.dylib                       0x03afb70d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
.

誰かが私が不明瞭な例外を得ているのか、それともそれを得ているのか、さらに良いの理由を私に言うことができるならば、私はとても感謝しています。事前にありがとう!

役に立ちましたか?

解決

これは、IBActionsまたはIBOutletsファイル内のstoryboardまたはxibの1つが正しく接続されていないことを意味します。それの名前はplayAudioで、WHGViewControllerという名前のビューコントローラの内側にあります。

2013-11-15 23:51:08.814 WhisperClient[51402:70b] 
***
  Terminating app due to uncaught exception 'NSUnknownKeyException',
  //Here it says that the error is in WHGViewController
  reason: '[<WHGViewController 0x9564950> setValue:forUndefinedKey:]:
      //Here it says that the IBAction/IBOutlet is named playAudio
      this class is not key value coding-compliant for the key playAudio.'
***
.

ビューコントローラに入るだけで、あなたのすべてのアウトレットとアクションが完了したものを確認してください。黄色い三角形が表示されている場合は、値が定義されていないことを意味し、View Controllerから削除する必要があります。

playAudioは定義されていませんが、WHGViewControllerはそれを使用しようとしているので、それがplayAudioを呼び出すその時点に取得されると、それが見つかりませんので、アプリは単にクラッシュします。

他のヒント

エラーは本質的にボタン用のiBoutletsがXIB /ストーリーボードと正しく接続されていないことを意味します。XIB /ストーリーボードのコンセントを取り外して再接続してみてください。それは問題を解決します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top