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
Xcode 7 Null passed to a callee that requires a non-null argument
提问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
。例如:
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 nil
in your method call, you already found the problem. If you pass a variable, check whether that is nil
at 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 __nullable
with completion.
我在使用自定义完成处理程序时遇到了这个问题。为处理程序传递 nil 值时发生警告。所以我添加__nullable
了完成。
typedef void (^ __nullable completionHandler)(void);