Objective C Tutorial - Objective C NSLog






NSLog method in Objective-C prints out log messages.

Example

#import <Foundation/Foundation.h>

int main()
{
   NSLog(@"Hello, World! \n");
   return 0;
}




Output String variable

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]){
   
    NSString *helloString = @"Hello World";
    NSLog(@"%@", helloString);
  
  return 0;
}

The code above generates the following result.





Output Object, character

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]){
    int myInteger = 1;
    NSLog(@"myInteger = %i", myInteger);
    
    float myFloatingPointNumber = 2;
    NSLog(@"myFloatingPointNumber = %f", myFloatingPointNumber);
    NSLog(@"myFloatingPointNumber in scientific notation = %e", myFloatingPointNumber);
    
    char myCharacter = 'A';
    NSLog(@"myCharacter = %c", myCharacter);
    
    //To print out the % symbol
    NSLog(@"Percent Sign looks like %%");
    
    //To print out Objective-C objects:
    NSString *myString = @"My String";
    NSLog(@"myString = %@", myString);
    NSLog(@"myString's pointer = %p", myString);
    
    //To print out a series of values
    NSLog(@"myCharacter = %c and myInteger = %i", myCharacter, myInteger);

  
  return 0;
}

The code above generates the following result.

Output Integer and String

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    
        
        NSString *myString1 = @"My String One";
        NSLog(@"myString1 = %@", myString1);
        
        NSString *myString2 = [NSString stringWithUTF8String:"My String Two"];
        NSLog(@"myString2 = %@", myString2);
        
        NSString *myString3 = [[NSString alloc] initWithUTF8String:"My String Three"];
        NSLog(@"myString3 = %@", myString3);
        
        int number = 4;
        NSString *myString4 = [NSString stringWithFormat:@"My String %i", number];
        NSLog(@"myString4 = %@", myString4);
     
    return 0;
}

The code above generates the following result.