方法和选择器有什么区别? [英] What's the difference between a method and a selector?

查看:108
本文介绍了方法和选择器有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Objective-C中的方法,选择器和消息之间有什么区别?

What the difference between a method, a selector and a message in Objective-C?

推荐答案

这是一个很好的问题.

  • 选择器-选择器是方法的名称.您对以下选择器非常熟悉:allocinitreleasedictionaryWithObjectsAndKeys:setObject:forKey:等.这就是我们确定此方法需要参数的方式.同样(尽管非常罕见),您可以具有如下选择器:doFoo:::.这是一个带有三个参数的方法,您可以像[someObject doFoo:arg1 :arg2 :arg3]那样调用它.不需要在选择器组件的每个部分之前都包含字母.就像我说的那样,这非常罕见,您不会在Cocoa框架中找到它.您可以直接在Cocoa中使用选择器.它们的类型为SEL:SEL aSelector = @selector(doSomething:)SEL aSelector = NSSelectorFromString(@"doSomething:");

  • Selector - a Selector is the name of a method. You're very familiar with these selectors: alloc, init, release, dictionaryWithObjectsAndKeys:, setObject:forKey:, etc. Note that the colon is part of the selector; it's how we identify that this method requires parameters. Also (though it's extremely rare), you can have selectors like this: doFoo:::. This is a method that takes three parameters, and you'd invoke it like [someObject doFoo:arg1 :arg2 :arg3]. There's no requirement that there be letters before each part of the selector components. As I said, this is extremely rare, and you will not find it used in the Cocoa frameworks. You can work with selectors directly in Cocoa. They have the type SEL: SEL aSelector = @selector(doSomething:) or SEL aSelector = NSSelectorFromString(@"doSomething:");

消息-消息是选择器,是您要随其发送的参数.如果我说[dictionary setObject:obj forKey:key],则消息"是选择器setObject:forKey:加上参数objkey.可以将消息封装在NSInvocation对象中,以供以后调用.邮件将发送到收件人. (即接收"消息的对象).

Message - a message is a selector and the arguments you are sending with it. If I say [dictionary setObject:obj forKey:key], then the "message" is the selector setObject:forKey: plus the arguments obj and key. Messages can be encapsulated in an NSInvocation object for later invocation. Messages are sent to a receiver. (ie, the object that "receives" the message).

方法-方法是选择器和实现(以及随附的元数据)的组合. 实现"是实际的代码块;它是一个函数指针(IMP).可以使用Method结构(可从运行时检索)在内部检索实际方法.

Method - a method is a combination of a selector and an implementation (and accompanying metadata). The "implementation" is the actual block of code; it's a function pointer (an IMP). An actual method can be retrieved internally using a Method struct (retrievable from the runtime).

您不需要的其他一些相关内容:

Some other related things that you didn't ask for:

  • 方法签名-方法签名表示方法返回和接受的数据类型.它们可以在运行时通过NSMethodSignature和(在某些情况下)原始的char*表示.

  • Method Signature - a method signature represents the data types returned by and accepted by a method. They can be represented at runtime via an NSMethodSignature and (in some cases) a raw char*.

实现-方法的实际可执行代码.它在运行时的类型是IMP,它实际上只是一个函数指针. iOS 4.3包含将块转换为IMP的新功能.这真的很酷.

Implementation - the actual executable code of a method. Its type at runtime is an IMP, and it's really just a function pointer. iOS 4.3 includes a new ability to turn a block into an IMP. This is really cool.

要实现的有趣事情之一是方法(选择器)的名称与方法(IMP)的实现不同.这意味着,如果您感到胆怯,可以将它们交换.您还可以在运行时添加和删除方法,因为您所做的只是编辑哈希表中的条目:键是选择器,值是方法的IMP.这使您可以做一些非常疯狂和令人迷惑的事情.不是为了胆小的人. :)

One of the fun things to realize is that the name of a method (the selector) is distinct from the implementation of the method (the IMP). This means that you can swap them around, if you're feeling daring. You can also add and remove methods at runtime, because all you're doing is editing an entry in a hash table: the key is the selector, and the value is the IMP of the method. This allows you to do some really crazy and trippy stuff. It's not for the faint of heart. :)

这篇关于方法和选择器有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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