NSLog

NSLog, Logging in Cocoa, Objective-C

NSLog logs an error message to the Apple System Log facility.

void NSLog (
NSString *format,

);

In the sample code given below we are using different format specifiers to print, integers, numbers, dates, float, boolean, arrays, dictionaries, strings etc.

Logging a function Name using NSLog

NSLog(@"The function is %s",__func__);
Output: The function is -[DDAppDelegate applicationDidFinishLaunching:]
NSLog(@"The FUNCTION is %s",__FUNCTION__);
Output: The FUNCTION is -[DDAppDelegate applicationDidFinishLaunching:]

NSLog size of primitive datatypes

//This test was done on a 64 BIT MAC
 NSLog(@"Size of char is  %zu byte", sizeof(char));        //Size of char is  1 byte
 NSLog(@"Size of short is  %zu  bytes", sizeof(short));    //Size of short is  2  bytes
 NSLog(@"Size of int is  %zu  bytes", sizeof(int));        //Size of int is  4  bytes
 NSLog(@"Size of long is  %zu  bytes", sizeof(long));      //Size of long is  8  bytes
 NSLog(@"Size of long long is  %zu  bytes", sizeof(long long)); //Size of long long is  8  bytes
 NSLog(@"Size of float is  %zu  bytes", sizeof(float));   //Size of float is  4  bytes
 NSLog(@"Size of double is  %zu  bytes", sizeof(double)); //Size of double is  8  bytes
 NSLog(@"Size of size_t is  %zu  bytes", sizeof(size_t)); //Size of size_t is  8  bytes

NSLog Primitive Datatypes

NSLog bool

    BOOL ksBool = YES;
    NSLog(@"ksBool is %d", ksBool);                     //ksBool is 1
    NSLog(@"ksBool is %@", ksBool? @"YES":@"NO");       //ksBool is YES

//Another approach
if (ksBool) {         
  NSLog(@"ksBool is True"); 
} 
else 
{ 
  NSLog(@"ksBool is False"); 
} 
Output: ddFlag is False

NSLog char

char charA = 'A';
NSLog(@"charA is %c",charA); //charA is A
NSLog(@"charA ASCII value is %hhd",charA); //charA ASCII value is 65
unsigned char unsignedChar = UCHAR_MAX;
NSLog(@"unsignedChar max value is %u", unsignedChar); //unsignedChar max value is 255

NSLog short

short signedShort = -32768;
 NSLog(@"signedShort is %hd",signedShort); //signedShort is -32768

NSLog Unsigned short

unsigned short unSignedShort = 65535;
 NSLog(@"unSignedShort is %hu",unSignedShort); //unSignedShort is 65535

NSLog Signed Int

int signedInt = pow(2,4*8-1);
 NSLog(@"signedInt min is %d",signedInt);     //signedInt min is -2147483648

NSLog Unsigned Int

unsigned int unsignedInt = pow(2,4*8) -1 ;
 NSLog(@"unsignedInt is %u", unsignedInt); //unsignedInt is 4294967295

NSLog Signed Long

long signedLong = pow (2,8*8-1);
 NSLog(@"signedLong is %ld",signedLong); //signedLong is -9223372036854775808

NSLog Unsigned Long

unsigned long unsignedLong =18446744073709551615;
 NSLog(@"unsignedLong is %lu",unsignedLong); //unsignedLong is 18446744073709551615

NSLog signed long long

//----- Use %lld

NSLog unsigned long long

//----- Use %llu

NSLog Float

float signedFloat = -109.7654321;
 NSLog(@"signedFloat %f",signedFloat); //signedFloat -109.765434
 NSLog(@"signedFloat 2 decimal point %.2f",signedFloat); //signedFloat 2 decimal point -109.77

NSLog Double

double doubleValue = -109.7654321;
 NSLog(@"doubleValue is %.2f",doubleValue); //doubleValue is -109.77
 NSLog(@"doubleValue is %e",doubleValue); //doubleValue is -1.097654e+02

NSLog Long Double

long double aLongDouble = -12.3456789e2L;
 NSLog(@"aLongDouble is %Lf", aLongDouble); //aLongDouble is -1234.567890
 NSLog(@"aLongDouble is %Le", aLongDouble); //aLongDouble is -1.234568e+03

NSLog NSNumber

NSNumber *charNum = [NSNumber numberWithChar:'A'];
 NSLog(@"charNum %@",charNum); //charNum 65
 NSLog(@"charNum charValue is %c", [charNum charValue]); //charNum charValue is A

NSLog String

NSString *string = @"This is not a very long string";
 NSLog(@"string is %@",string); //string is This is not a very long string

NSLog Rect, NSLog point, NSLog Size

NSRect rect = NSMakeRect(10,20,100,100);
NSLog(@"rect dimensions are %@",NSStringFromRect(rect) );
NSLog(@"x=%0.1f , y =%0.1f, width = %0.1f, height =%0.1f",rect.origin.x, rect.origin.y,rect.size.width,rect.size.height);
NSPoint point = NSMakePoint(100.0, 200.0);
NSLog(@"point positions are %@",NSStringFromPoint(point));
NSSize size = NSMakeSize(100.0, 400.0);
NSLog(@"size dimensions are %@",NSStringFromSize(size));
Output:
rect dimensions are {{10, 20}, {100, 100}}
x=10.0 , y =20.0, width = 100.0, height =100.0
point positions are {100, 200}
size dimensions are {100, 400}

NSLog Date


NSDate *calDate = [NSCalendarDate calendarDate];
NSDate *date = [NSDate date];
NSLog(@"Calendar Date is in Local Time %@",calDate);
Output: Calendar Date is in Local Time 2013-10-20 13:29:04 -0700
NSLog(@"Todays date is in GMT %@",date);
Output: Todays date is in GMT 2013-10-20 20:29:04 +0000

NSLog float

float floatValue = 10923874431.988;
 NSLog(@"float floatValue using f specifier %f",floatValue);
 NSLog(@"float floatValue using e specifier %e",floatValue);
 NSLog(@"float floatValue using E specifier %E",floatValue);
 NSLog(@"float floatValue using g specifier %g",floatValue);
 NSLog(@"float floatValue using G Specifier %G",floatValue);
Output: float floatValue using f specifier 10923874304.000000 
 Output: float floatValue using e specifier 1.092387e+10
 Output: float floatValue using E specifier 1.092387E+10
 Output: float floatValue using g specifier 1.09239e+10
 Output: float floatValue using G Specifier 1.09239E+10

NSLog NSInteger

NSInteger integer = 42;
NSLog(@"integer %ld",(long)integer);
Output: integer 42

NSLog dictionary

//Printing the Contents Of a dictionary
NSArray *keys = [NSArray arrayWithObjects:@"firstName", @"lastName", @"email", nil]; 
NSArray *objects = [NSArray arrayWithObjects:@"John", @"Doe", @"jDoe@knowstack.com", nil];

NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSLog(@"dictionary %@",dictionary);
Output:dictionary {
 email = "jDoe@knowstack.com";
 firstName = John;
 lastName = Doe;
 }
for (id key in dictionary) {
NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}

Output:key: firstName, value: John
Output:key: email, value: jDoe@knowstack.com
Output:key: lastName, value: Doe

NSLog array

//Printing the content of an Array
NSArray *dataArray = [NSArray arrayWithObjects:@"John",@"Doe",@"JohnDoe@knowStack.com", nil];
NSLog(@"dataArray %@",dataArray);

Output:
dataArray (
John,
Doe,
"JohnDoe@knowStack.com"
)
NSLog(@"dataArray %@",[dataArray objectAtIndex:2]);
Output: dataArray JohnDoe@knowStack.com

Also Read, Swift print, println and NSLog

Reference

More details on format specifiers can be found at https://developer.apple.com/library/ios/documentation/cocoa/conceptual/Strings/introStrings.html#//apple_ref/doc/uid/10000035-SW1 https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSLogv

Posted in Cocoa, iOS, Objective C Tagged with: , , , , ,
5 comments on “NSLog
  1. Ruby Alves says:

    If some one needs to be updated with most recent technologies then he must be pay a visit this site and
    be up to date all the time.

  2. Rme8Up I appreciate, cause I found exactly what I was looking for. You’ve ended my 4 day long hunt! God Bless you man. Have a great day. Bye

  3. Thanks again for the blog article.Really looking forward to read more. Much obliged.

  4. eebest8 fiverr says:

    This blog was how do I say it? Relevant!! Finally I ave found something which helped me. Thanks a lot.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts


Hit Counter provided by technology news