jovany
jovany

Reputation:

simple question concerning NSString adding multiple strings

I have a fairly simple question concerning NSString however it doesn't seem to do what I want.

this is what i have

NSString *title = [NSString stringWithformat: character.name, @"is the character"];

This is a line in my parser takes the charactername and inserts in into a plist , however it doesn't insert the @"is the character" is there something I'm doing wrong?

Upvotes: 1

Views: 3427

Answers (2)

IlDan
IlDan

Reputation: 6869

Your code is wrong. It should be :

NSString *title 
    = [NSString stringWithformat:@"%@ is the character", character.name];

assuming that character.name is another NSString.

Read the Formatting String Objects paragraph of the String Programming Guide for Cocoa to learn everything about formatting strings.

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 881243

stringWithFormat takes a format string as the first argument so, assuming character.name is the name of your character, you need:

NSString *title = [NSString stringWithformat: @"%s is the character",
    character.name];

What you have is the character name as the format string so, if it's @"Bob" then Bob is what you'll get. If it was "@Bob %s", that would work but would probably stuff up somewhere else that you display just the character name :-)

Note that you should use "%s" for a C string, I think "%@" is the correct format specifier if character.name is an NSString itself.

Upvotes: 0

Related Questions