像NSNumber一样装箱NSString stringWithFormat [英] Boxing of NSString stringWithFormat like NSNumber

查看:68
本文介绍了像NSNumber一样装箱NSString stringWithFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以这样创建一个NSNumber

We can create an NSNumber like this

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

我知道我们可以使用以下语句转换为NSString

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

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

但是,我们可以创建这样的NSString吗?

However, can we create an NSString like this?

NSString *string = @(@"Name is:%@",name); //equivalent of [NSString stringWithFormat]

推荐答案

这只是我的脑袋. 我不认为这有句语法糖.

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:

将其放入您的.pch文件中

Put this in your .pch file

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

并命名为:

NSString *s = format(@"%@, %@", @"a", @"b");

这篇关于像NSNumber一样装箱NSString stringWithFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆