Using Foundation’s NSLog Function

NSLog is a Foundation function that allows you to print debugging output to the console. This works much like the following C function.

CODE:
  1. fprintf(stderr, format_string, args ...);

NSLog takes in a format string that is actually an NSString. You can use it in the usual ways such as the following.

OBJC:
  1. NSNumber *number = [NSNumber numberWithInt:5];
  2. int num = 4;
  3. NSLog(@"number = %@", number);
  4. NSLog(@"num = %i", num);

The above code will output the following


number = 5

num = 4

NSLog will accept the following format specifiers.

Format Specifier Description
 
%@ Cocoa Object responding to -description
%d, %i Signed Integer
%u Unsigned Integer
%f Float / Double
%x, %X Hexidecimal Integer
%o Octal Integer
%zu size_t
%p Pointer
%e Float / Double (in scientific notation)
%g Float / Double (as %f or %e, based on value)
%s C String
%.*s Pascal String
%c Character
%C Unicode Character
%ll Long Long
%llu Unsigned Long Long
%Lf Long Double

NSLog() comes in very handy when you are debugging your code, it allows you to place messages throughout your code and see the messages while your application is running.


Technology Blogs Add to Technorati Favorites Page Rank Tool NYPHP Users Group View Joseph Crawford's profile on LinkedIn

   

SEO Consultant SEO services