Objective-C – Format Specifier in NSString

Ususally we use the Format Specifier in NSLog() to show the debug output. For Example:

NSString* name = @"Yuen Ying Kit";
int age = 26;
NSLog(@"My name is %@", name);
NSLog(@"I am %i years old", age);

 

You will get a debug output like

 

List of Format Specifiers

%@     NSString*
%d     Signed Integer
%i     Signed Integer
%u     Unsigned Integer
%f     Float / Double
%x     Hexidecimal Integer
%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

Reference: Using Foundation’s NSLog Function

Done =)

6 thoughts on “Objective-C – Format Specifier in NSString”

  1. It’s also possible to create strings by format…

    int var = 3;
    NSString * example = [NSString stringWithFormat:@”This is an example, var: %d”, var];
    NSLog(example);

    Output: This is an example, var: 3

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.