`
chriszeng87
  • 浏览: 718042 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Objective C中message,method和selector等的区别和联系

    博客分类:
  • iOS
iOS 
阅读更多

   selector是方法名,message包括selector和方法的参数,method包括selector和方法的具体实现。

 

以下转自stackoverflow: http://stackoverflow.com/questions/5608476/whats-the-difference-between-a-method-and-a-selector

 

  • Selector - a Selector is the name of a method. You're very familiar with these selectors: alloc,initreleasedictionaryWithObjectsAndKeys: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 SELSEL aSelector = @selector(doSomething:) or SEL aSelector = NSSelectorFromString(@"doSomething:");

  • 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 selectorsetObject:forKey: plus the arguments obj and key. Messages can be encapsulated in anNSInvocation object for later invocation. Messages are sent to a receiver. (ie, the object that "receives" the message).

  • 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).

 

1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics