Understanding NSLog syntax

37,104

Solution 1

I agree this is pretty confusing when you're starting out. The main reason is that the NSLog method, like many others in Core Foundation, is a C-based API, rather than an Objective-C API. C-style functions look like this myFunction(myParameter1, myParameter2).

All the GUI stuff you're probably used to [UIView presentModalViewController:] etc is based around an Objective-C API, with the square brackets that you've seen for functions (called selectors in Obj-C) . The Objective-C language sits on top of C, so you will find both styles in most apps.

Solution 2

As I understand it, NSLog isn't an Objective C function but a C function built into the foundation of Cocoa. Therefore it conforms to basic C functions with variadic arguments.

Solution 3

NSLog may seem like a class, but it isn't.

NSLog is a FoundationKit function for printing debug statements to the console. It is defined in NSObjCRuntime.h:

void NSLog(NSString format, ...);

There is a good amount of information here: http://cocoadev.com/wiki/NSLog

EDIT: As @fyngyrz pointed out, the page is dead. So here is a wayback-machine version of the page from 2012

Share:
37,104
tahir
Author by

tahir

Updated on September 20, 2020

Comments

  • tahir
    tahir almost 4 years

    (I'm a cocoa beginner and ) I'm wondering why we should do:

    NSLog(@"this is the variable value: %d",variable);
    

    and not something like this:

    [NSLog outputThis:@"this is the variable value: %d" param:variable];
    
  • onmyway133
    onmyway133 almost 10 years
    Apple decides everything, and we 'll like it
  • fyngyrz
    fyngyrz almost 7 years
    No, there isn't. The page is gone.
  • Alladinian
    Alladinian almost 7 years
    @fyngyrz Thanx. Edited the answer.
  • Pranav Kasetti
    Pranav Kasetti over 3 years
    ^ is the story of every iOS developer XD