Objective-C stringWithFormat

April 8, 2009 § Leave a comment

You can create a NSString by creating a ‘static” instance by using @”…” construct.

NSString *variable = @”Sample”;

You can also create NSString using class method +stringWithFormat:. It functions similar to the printf function of the standard C library. It didn’t send to the standard output, instead it is put in a NSString.

int players = 50;
NSString *game;
game = [NSString stringWithFormat: @”This game accepts %d players”, players];

game will return a NSString containing “This game accepts 50 players”.

You can also use %@ specification to output a description of an object using NSObject -description for debugging.

NSObject *object = [myObject myMethod];
NSLog (@”The method returned: %@”, object);


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

What’s this?

You are currently reading Objective-C stringWithFormat at Web Builders.


%d bloggers like this: