是否可以将NULL传递给块参数? [英] Is it okay to pass in NULL to a block parameter?

查看:81
本文介绍了是否可以将NULL传递给块参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将 NULL nil 传入<$ c中的完成块时,iOS不会对我尖叫$ c> animateWithDuration:动画:完成:但这是否意味着没关系?或者最好打开一个空的 ^ {}

iOS does not scream at me when I pass in NULL or nil to the completion block in animateWithDuration:animations:completion: but does that mean it's okay? Or is it better to open an empty ^{ }?

推荐答案

这是可以的,只要您可以相信您传递 nil 的代码不会尝试将其称为块。

This is okay as long as you can trust that the code to which you are passing the nil won't try to call it as a block.

快速演示:

typedef void (^GenericBlock)(void);

void useThisBlock(GenericBlock block){
    block();
}

useThisBlock(^{NSLog(@"All okay.");});
useThisBlock(nil);    // Compiles but crashes

内部代码必须首先检查块: if (块)block();

The inner code must check the block first: if( block ) block();

对于UIKit代码,你应该没事。

In the case of UIKit code, you should be fine.

这篇关于是否可以将NULL传递给块参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆