Xcode 7 Null传递给需要非null参数的被调用者

Uyg*_*lan 8 xcode ios

我更新了xcode 7并给出了这个错误

Null传递给需要非null参数的被调用者

 _recorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.%@", [NSHomeDirectory() stringByAppendingString:@"/Documents"], name, extension]] settings:nil error:nil];
Run Code Online (Sandbox Code Playgroud)

rui*_*ano 31

如果困扰你的是警告,你可以使用它来压制它 -Wnonnull

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"

_recorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.%@", [NSHomeDirectory() stringByAppendingString:@"/Documents"], name, extension]] settings:nil error:nil];

#pragma clang diagnostic pop
Run Code Online (Sandbox Code Playgroud)


Rai*_*rze 2

一种简单的检查方法是使用Show Completions- 转到方法名称并按Ctrl-Space菜单Editor>中的 或Show Completions。将会弹出一个窗口。查找带有(nonnull)- 这些不能是的条目nil。例如:

(非空)示例弹出窗口

Ctrl-Space用光标按了[NSString stringWithFormat:...]。正如您所看到的,很多参数都被标记了(nonnull)

当你显式传入nil你的方法调用时,你就已经发现了问题。如果你传递一个变量,检查它是否是nil在那个时间。