是否可以通过内容比较两个Objective-C块? [英] Is it possible to compare two Objective-C blocks by content?

查看:116
本文介绍了是否可以通过内容比较两个Objective-C块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

float pi = 3.14;
float (^piSquare)(void) = ^(void){ return pi * pi; };
float (^piSquare2)(void) = ^(void){ return pi * pi; };

[piSquare isEqualTo: piSquare2]; // -> want it to behave like -isEqualToString...


推荐答案

展开Laurent的回答。

To expand on Laurent's answer.

块是实现和数据的组合。对于两个相等的块,它们将需要具有完全相同的实现并且捕获完全相同的数据。因此,比较需要比较实现和数据。

A Block is a combination of implementation and data. For two blocks to be equal, they would need to have both the exact same implementation and have captured the exact same data. Comparison, thus, requires comparing both the implementation and the data.

有人可能会认为比较实现会很容易。它实际上不是因为编译器的优化器的工作原理。

One might think comparing the implementation would be easy. It actually isn't because of the way the compiler's optimizer works.

虽然比较简单的数据是相当简单的,块可以捕获对象 - 包括C ++对象工作有一天) - 和比较可能或可能不需要考虑到这一点。一个简单的实现只是对捕获的内容进行字节级比较。然而,也可能希望使用对象级别比较器来测试对象的等同性。

While comparing simple data is fairly straightforward, blocks can capture objects-- including C++ objects (which might actually work someday)-- and comparison may or may not need to take that into account. A naive implementation would simply do a byte level comparison of the captured contents. However, one might also desire to test equality of objects using the object level comparators.

然后是__block变量的问题。块本身实际上没有与__block捕获的变量相关的任何元数据,因为它不需要它来满足所述变量的要求。因此,比较不能比较__block值,而不显着改变编译器代码。

Then there is the issue of __block variables. A block, itself, doesn't actually have any metadata related to __block captured variables as it doesn't need it to fulfill the requirements of said variables. Thus, comparison couldn't compare __block values without significantly changing compiler codegen.

这就是说,不,当前不可能比较块和概述一些原因。如果您认为这样做有用,请通过 http://bugreport.apple.com/ 提交错误。并提供一个用例。

All of this is to say that, no, it isn't currently possible to compare blocks and to outline some of the reasons why. If you feel that this would be useful, file a bug via http://bugreport.apple.com/ and provide a use case.

这篇关于是否可以通过内容比较两个Objective-C块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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