标记“无Objective-C块"的哪一个正确(nil或NULL)? [英] Which is the right one, nil or NULL, to mark "no Objective-C block"?

查看:54
本文介绍了标记“无Objective-C块"的哪一个正确(nil或NULL)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我不希望为Objective-C块传递任何内容,应该使用什么关键字NULLnil?我之所以这样问是因为,Objective-C块是一个Objective-C对象(据我所知),但是表示为一个函数指针.

NULLnil都指示0x0指针,但是它们在语义上有所不同.所以我对此很担心.

解决方案

块未表示为函数指针.它们表示为块,并在其声明中用^符号表示.在幕后,唯一的相似之处是调用语法.否则,它们都是非常非常不同的.

在它们上调用方法通常很有用.例如,如果您不使用垃圾回收,那么您需要要在块上调用copy方法(如果要保留它们以后再使用).随着自动保留计数的出现,这甚至是复制块的唯一方法,因为ARC指针强制转换规则阻止您使用Block_copy宏.

NULL取决于您的编译器,只是0(void*)0.这将适用于任何类型的指针.但是,由于Objective-C的语言规则,如果您尝试将消息发送到无法直接转换为id的类型(如果使用ARC则出错),则会收到警告.

由于将消息发送到块可能很有用,因此应该对它们使用nil.


很明显,使用nilNULL将导致完全相同的二进制代码.您可以根据是否将块视为Objective-C对象来选择常数.在您不得不编写自己的retainrelease调用的日子里,这是一笔更大的交易,但是现在ARC可以为您完成所有内存管理工作.

如果在使用ARC之前还使用过块,您可能会认为它们是Objective-C对象.如果不是,那么您可能不会想到/意识到它们是.在该语言中,仍然有一些关于其身份的遗物(例如,您可以使用__weak__unsafe_unretained指向块的指针),但是在大多数情况下,差异可以忽略不计.选择一个并尝试坚持下去.我喜欢将我的块视为Objective-C对象.

If I want to pass nothing for an Objective-C block, what keyword should I use, NULL or nil? I'm asking this because an Objective-C block is an Objective-C object (as I know), but represented as a function pointer.

NULL and nil both indicate a 0x0 pointer, however they are different semantically. So I'm concerned about this.

解决方案

Blocks are not represented as function pointers. They're represented as blocks, and this is denoted by the ^ symbol in their declaration. Down under the hood, the only resemblance is the call syntax. Otherwise, they are both very, very different.

It is often useful to call methods on them. For instance, if you don't use garbage collection, you need to call the copy method on blocks if you want to keep them for later. With the advent of automatic retain count, this is even the only way to copy a block, since ARC pointer cast rules prevent you from using the Block_copy macro.

NULL is, depending on your compiler, either just 0 or (void*)0. This would work for any kind of pointer. However, because of the language rules of Objective-C, you'll get a warning if you try to send a message to a type that can't cast directly to id (and an error if you use ARC).

Since it can be useful to send messages to blocks, you should use nil for them.


[EDIT] Let's be clear that using either nil or NULL will result in exactly the same binary code. Your choice of the constant should probably be based on whether you consider blocks to be Objective-C objects or not. This was a bigger deal back in the days when you had to write your own retain and release calls, but now ARC does all the memory management work for you.

If you've used blocks before ARC was a thing, you probably think that they're Objective-C objects. If not, then you probably don't think/realize that they are. There are still a few relics of their identity in the language (for instance, you can have a __weak or __unsafe_unretained pointer to a block), but for the most part, the difference is negligible. Pick one and try to stick with it. I like to think of my blocks as Objective-C objects.

这篇关于标记“无Objective-C块"的哪一个正确(nil或NULL)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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