Skip to content

Instantly share code, notes, and snippets.

@supermarin
Created December 5, 2012 22:14
Show Gist options
  • Save supermarin/4219990 to your computer and use it in GitHub Desktop.
Save supermarin/4219990 to your computer and use it in GitHub Desktop.
NSString literals
// Currently, there's too much effort for printing strings with arguments.
// The current way
NSString *sentence = [NSString stringWithFormat:@"This is a text with argument %@", argument];
// Possible literal
NSString *sentence = @("This is a text with argument %@", argument);
@neilco
Copy link

neilco commented Dec 5, 2012

I like the way Ruby does it. The ObjC equivalent would be:

NSString *sentence = @"This is a %@ with argument %@" % obj, arg;

@supermarin
Copy link
Author

@neilco correct, but I'm not sure the compiler would be able to do it and maintain compatibility with C.

@umairsd
Copy link

umairsd commented Dec 6, 2012

Wouldn't the compiler be more comfortable with:

NSString *sentence = @(@"This is a text with argument %@", argument);

@S2Ler
Copy link

S2Ler commented Dec 6, 2012

Have you tried to ask Apple for this new feature? :)

@supermarin
Copy link
Author

@umairsd correct, that's the original proposal :)

@dilejmon, I haven't; wanted to see what others are saying. I don't know either what's the best place to ask this question :)
In the meantime, @neilco has built a macro _(@"text: %@", argument) , but I find it a bit confusing for newcomers.

We ended up with NSStringWithFormat(@"text: %@", argument) , you can find it in ObjectiveSugar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment