Understanding NSLog syntax

tahir picture tahir · Jun 25, 2012 · Viewed 36.8k times · Source

(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];

Answer

Ben Clayton picture Ben Clayton · Jun 25, 2012

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.