'NSInvalidArgumentException',原因:'*** -[NSURL initFileURLWithPath:]: nil 字符串参数

'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter

我只是在为我的游戏添加音效,在我输入这段代码之前它运行良好:

if (hit == NO) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"miss" ofType:@"m4a"];
        AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
        [theAudio play];
        NSLog(@"audio played miss.m4a");
        // if hit is no miss sound effect plays

    }
    else {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"hit" ofType:@"m4a"];
        AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
        [theAudio play];
        NSLog(@"audio played hit.m4a");
        //if hit is yes or else (not no) then it plays hit sound effect
    }

它说问题出在 [NSURL fileURLWithPath:path] 部分,我已经检查过它并查看了许多问题和教程,但似乎找不到解决方案。我的所有文件都在 Xcode 项目中并且命名正确并且对应于我的 pathForResource:@"miss" 和 hit 以及 ofType 等

使用断点检查路径,如果为 nil,则可能资源不在包中或具有不同的名称。
请注意,虽然模拟器不区分大小写,但设备区分大小写。