• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Introspecting the current method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Introspecting the current method


  • Subject: Re: Introspecting the current method
  • From: Dave DeLong <email@hidden>
  • Date: Sun, 18 Apr 2010 20:15:40 -0600

Yes, code should obviously be written with this knowledge in mind.  The use case I have for it is for macros.  I like to use a debugging macro like the following to ensure that methods are getting called (without having to break execution to stop at a breakpoint):

#define LogMethod NSLog(@"-[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd))

This, of course, is only accurate for instance methods (since I'm logging a "-").  I was just wondering if there was a way I could use some sort of introspection to appropriately place a + or a -.

I like the ([self class] == self) method, simply because it's shorter, but the [[self class] instancesRespondToSelector:_cmd] is also a great solution.

Thanks for the ideas!

Dave

On Apr 18, 2010, at 6:21 PM, Graham Cox wrote:

>
> On 19/04/2010, at 10:14 AM, Dave DeLong wrote:
>
>> If I'm inside a method, is there a way to know at runtime whether that method is a class or an instance method?  Currently the only way I though of to do this is to see if "self" is a Class object or not, but I was wondering if there's a more reliable way to determine this.
>
>
> I might be lacking imagination here, but I can't think of any situation where needing to detect this would make any sense. Methods should be written with this knowledge built-in to them, since they can either function as class methods or instance methods but not both. And client code should also know what it's doing and will call the appropriate method for whatever task it wants the object to carry out.
>
> You could ask whether [[self class] instancesRespondToSelector:_cmd] and if it returns YES it's an instance method. But I'm boggling trying to think of a legitimate use case.
>
> --Graham
>
>

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Introspecting the current method
      • From: Michael Ash <email@hidden>
References: 
 >Introspecting the current method (From: Dave DeLong <email@hidden>)
 >Re: Introspecting the current method (From: Graham Cox <email@hidden>)

  • Prev by Date: Blend mode 'darken' for NSImage?
  • Next by Date: Re: Introspecting the current method
  • Previous by thread: Re: Introspecting the current method
  • Next by thread: Re: Introspecting the current method
  • Index(es):
    • Date
    • Thread