Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the macro</p> <pre><code>#define debug_print(expr...) NSLog(@"%@ is %@", @#expr, (expr)) </code></pre> <p>As you can see, the NSLog format string expects a %@ (object), so you need to use a variant for integers:</p> <pre><code>#define debug_printI(expr...) NSLog(@"%@ is %d", @#expr, (expr)) </code></pre> <p><strong>EDIT:</strong></p> <p>There is a way to use a single macro and have it work with all types: use @encoding to find the type.</p> <pre><code>NSString *print_debug_format_for_type(const char *encoding) { switch (encoding[0]) { case '@': return @"%@ is %@"; case '*': return @"%@ is %s"; case 'f': return @"%@ is %f"; case 'd': return @"%@ is %f"; case 'q': return @"%@ is %qi"; case 'Q': return @"%@ is %qu"; case 'c': case 'i': case 's': default: return @"%@ is %d"; } } #define debug_print(expr...) ({ \ typedef __typeof__(expr) t; \ NSLog(print_debug_format_for_type(@encode(t)), @#expr, (expr)); \ }) </code></pre> <p>With this, you can use debug_print anywhere with (almost) any type, as long as it's in the print_debug_format_for_type. The encodings that are passed in (from @encode) can be found <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html" rel="nofollow">here</a> [apple docs], and the formats for the format string can be found <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1" rel="nofollow">here</a> [also apple docs]. As is, this works with any object, c-style string, integer, float, or double expression.</p> <p>(minor caveat: the ObjC BOOL type is actually a typedef'd char, so if you print_debug a boolean expression it will say it is a 1 or 0. While this works, it also means that if you print_debug a char expression, it will say it is the ascii number of the char. To change this behavior (and break BOOL printing), change case 'c' to <code>return @"%@ is %c"</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload