Tom Baker as Doctor Who, the 4th Doctor

If you’re coming from PHP or Javascript, you may be intrigued by Objective-C’s style of displaying variables and objects in strings.

Luckily, if you’ve ever had to do any C coding in your life, you’ll recognize Objective-C’s style. It uses proper C-style string format specifiers.  (If you know Python, it is also very similar to Python string formatting).

How does it work?

You simply substitute a placeholder for the method or object in your string, followed by the name of the objects and properties you would like those placeholders to replace.

For example:

NSString *actorName = @”Tom Baker”;
int doctorNumber = 4;
NSLog(@”You may remember that %@ was the %ith Doctor!”, actorName, doctorNumber);


You may remember that Tom Baker was the 4th Doctor!

Note that the “%i” placeholder was used for an integer, while the “%@” was used for the string. You’ll want to use “%@” for all objects, including NSIntegers. Another popular placeholder is “%f“, used for both floats and doubles.

The “%@” placeholder tells Objective-C to display an object’s “Description” property, which you can easily override.

The above code could be equivalent to the Python string formatting:

actor_name = “Tom Baker” doctor_number = 4 print “You may remember that %s was the %ith Doctor!” % (actor_name, doctor_number)

In Ruby

actor_name = “Patrick Troughton” doctor_number = 2 puts “You may remember that #{actor_name} was the #{doctor_number}nd Doctor!”

In C++:

cout << “You may remember that  ” << actorName << ”  was the ” << doctorNumber << “th Doctor!.” << endl;

In JavaScript:

console.log(“You may remember that ” + actorName + ” was the ” + doctorNumber + “th Doctor!”);


echo “You may remember that {$actorName} was the {$doctorNumber}th Doctor!”;

For practice, try replacing this data with your favorite Doctor Who actor.

You have a favorite Doctor, right?!

 App Development, Development

, , , , , , ,

Matthew's Blog
Home   Blog   LinkedIn   Contact Me
Connect on LinkedIn, Google+,
Current focus: App & Web Development
Preferred Environments: OS X, iOS, Xcode, Sublime Text
Current skills: SWIFT, Objective-C, iOS SDK, CoreData, CoreAudio, JavaScript, HTML5, CSS, Wordpress
Degree: Bachelors of Science in Computer Information Systems Technology
from PURDUE University

© Website by Matthew Fecher
Denver, Colorado, USA.