Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boxing of NSString stringWithFormat like NSNumber

We can create an NSNumber like this

NSNumber *number = [NSNumber numberWithFloat:4.5];
//or
NSNumber *number = @(4.5);
//or
NSNumber *number = @4.5;

I know we can convert to an NSString with the following statement

NSString *string = @("stuff"); //equivalent of [NSString stringWithUTF8String]

However, can we create an NSString like this?

NSString *string = @(@"Name is:%@",name); //equivalent of [NSString stringWithFormat]
like image 705
thandasoru Avatar asked Jul 17 '14 04:07

thandasoru


1 Answers

This is just off the top of my head. I do not think there's any syntactic sugar for this.

Though, I believe you could achieve what you are looking for like this:

Put this in your .pch file

#define format(s, ...) 
[NSString stringWithFormat:s, ##__VA_ARGS__]

And call it :

NSString *s = format(@"%@, %@", @"a", @"b");
like image 150
Eyeball Avatar answered Nov 09 '22 01:11

Eyeball