Boxing of NSString stringWithFormat like NSNumber

浪尽此生 提交于 2019-12-19 21:59:13

问题


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]

回答1:


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");


来源:https://stackoverflow.com/questions/24794816/boxing-of-nsstring-stringwithformat-like-nsnumber

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!