Xcode 7 Null 传递给需要非空参数的被调用方

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32677389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:53:04  来源:igfitidea点击:

Xcode 7 Null passed to a callee that requires a non-null argument

iosxcode

提问by Uyghur Ilan

i updated xcode 7 and gives this error

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

Null passed to a callee that requires a non-null argument

Null 传递给需要非空参数的被调用方

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

回答by ruiaureliano

If what bothers you is the warnings, you can supress that using this -Wnonnull

如果让你烦恼的是警告,你可以使用这个来抑制它 -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

回答by Rainer Schwarze

An easy way to check is to use Show Completions- go to a method name and press Ctrl-Spaceor in the menu Editor> Show Completions. A window will pop up. Look for entries with (nonnull)- these must not be nil. For example:

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

(nonnull) sample popup

(非空)示例弹出窗口

I pressed Ctrl-Spacewith the cursor in [NSString stringWithFormat:...]. As you can see a lot of arguments are marked (nonnull).

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

When you explicitly pass nilin your method call, you already found the problem. If you pass a variable, check whether that is nilat that time.

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

回答by Pooja krishna p.b

I got this issue while using a custom completion handler . The warning occured while passing nil value for the handler. So I have added __nullablewith completion.

我在使用自定义完成处理程序时遇到了这个问题。为处理程序传递 nil 值时发生警告。所以我添加__nullable了完成。

typedef void (^ __nullable completionHandler)(void);