Objective-C块与选择器.哪一个更好? [英] objective-c block vs selector. which one is better?

查看:98
本文介绍了Objective-C块与选择器.哪一个更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在Objective-c中,当您实现要执行重复操作的方法时,您需要在该语言为您带来的多个选项之间进行选择:

In objective-c when you are implementing a method that is going to perform a repetitive operations, for example, you need to choice in between the several options that the language brings you:

@interface FancyMutableCollection : NSObject { }
-(void)sortUsingSelector:(SEL)comparator;
// or ...
-(void)sortUsingComparator:(NSComparator)cmptr;
@end

我想知道哪个是更好?

I was wondering which one is better?

Objective-c提供了许多选项:选择器,块,指向函数的指针,符合协议的类的实例等.

Objective-c provides many options: selectors, blocks, pointers to functions, instances of a class that conforms a protocol, etc.

有时候选择很明确,因为只有一种方法可以满足您的需求,但是其余的呢?我不希望这仅仅是时尚问题.

Some times the choice is clear, because only one method suits your needs, but what about the rest? I don't expect this to be just a matter of fashion.

是否存在任何规则来知道何时使用选择器以及何时使用块?

推荐答案

我能想到的主要区别是,对于块而言,它们的作用就像闭包一样,因此它们捕获了范围内的所有变量.如果您已经在那里有变量,并且不想创建一个实例变量只是为了暂时保存该变量,以便操作选择器在运行时可以访问它,这将是一个好选择.

The main difference I can think of is that with blocks, they act like closures so they capture all of the variables in the scope around them. This is good for when you already have the variables there and don't want to create an instance variable just to hold that variable temporarily so that the action selector can access it when it is run.

关于集合,如果系统中有多个内核,则块具有同时运行的附加功能.目前在iPhone中还没有,但iPad 2确实有,而且未来的iPhone型号可能会具有多个内核.在这种情况下,使用块可让您的应用在将来自动扩展.

With relation to collections, blocks have the added ability to be run concurrently if there are multiple cores in the system. Currently in the iPhone there isn't, but the iPad 2 does have it and it is probable that future iPhone models will have multiple cores. Using blocks, in this case, would allow your app to scale automatically in the future.

在某些情况下,块也更容易阅读,因为回调代码就在回调它的代码旁边.当然,情况并非总是如此,但有时有时确实会使代码更易于阅读.

In some cases, blocks are just easier to read as well because the callback code is right next to the code that's calling it back. This is not always the case of course, but when sometimes it does simply make the code easier to read.

很抱歉请您参考文档,但是要获得关于块的优缺点的更全面的概述,请查看

Sorry to refer you to the documentation, but for a more comprehensive overview of the pros/cons of blocks, take a look at this page.

如苹果所说:

块通常代表小的独立代码段.因此,它们特别有用,可以封装可以同时执行的工作单元,也可以封装集合中的项,也可以封装其他操作完成后的回调.

Blocks represent typically small, self-contained pieces of code. As such, they’re particularly useful as a means of encapsulating units of work that may be executed concurrently, or over items in a collection, or as a callback when another operation has finished.

块是传统回调函数的有用替代方法,主要有两个原因:

Blocks are a useful alternative to traditional callback functions for two main reasons:

它们允许您在调用点编写代码,该代码稍后在方法实现的上下文中执行. 因此,块通常是框架方法的参数.

They allow you to write code at the point of invocation that is executed later in the context of the method implementation. Blocks are thus often parameters of framework methods.

它们允许访问局部变量. 您不必直接使用局部变量,而不必使用需要数据结构来体现执行操作所需的所有上下文信息的回调.

They allow access to local variables. Rather than using callbacks requiring a data structure that embodies all the contextual information you need to perform an operation, you simply access local variables directly.

查看全文

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