• 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: Michael Ash <email@hidden>
  • Date: Mon, 19 Apr 2010 10:09:26 -0400

On Mon, Apr 19, 2010 at 3:27 AM, Jean-Daniel Dupas
<email@hidden> wrote:
>
> Le 19 avr. 2010 à 04:21, Michael Ash a écrit :
>
>> On Sun, Apr 18, 2010 at 10:15 PM, Dave DeLong <email@hidden> wrote:
>>> 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.
>>
>> The magic __func__ identifier produces a C string which I believe, in
>> an ObjC method, has exactly the format you're looking for.
>>
>> Mike
>
> It is fine for most cases, but __func__ is defined at compile time, and so, does not log the real type of the instance (if this is a subclass).

For me, if you're using this in a logging function, this is a feature,
not a bug. If I'm logging a message like "X is Y, should be Z" then I
want to know what code triggered that, and logging the actual object
type is much less useful for that. If you're interested in the actual
type as well, then I'd say just log that too, like:

NSLog(@"%s:%d %@: %@", __func__, __LINE__, self, yourMessageGoesHere);

Mike
_______________________________________________

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: Greg Parker <email@hidden>
    • Re: Introspecting the current method
      • From: Roy Lovejoy <email@hidden>
References: 
 >Introspecting the current method (From: Dave DeLong <email@hidden>)
 >Re: Introspecting the current method (From: Graham Cox <email@hidden>)
 >Re: Introspecting the current method (From: Dave DeLong <email@hidden>)
 >Re: Introspecting the current method (From: Michael Ash <email@hidden>)
 >Re: Introspecting the current method (From: Jean-Daniel Dupas <email@hidden>)

  • Prev by Date: Re: blocks and autorelease weirdness
  • 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