ios - 像 NSNumber 这样的 NSString stringWithFormat 的装箱

标签 ios objective-c nsstring

我们可以像这样创建一个 NSNumber

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

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

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

但是,我们可以像这样创建一个 NSString 吗?

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

最佳答案

这只是我的头脑。 我认为这没有任何语法糖分。

虽然,我相信你可以像这样实现你正在寻找的东西:

将其放入您的 .pch 文件

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

并称它为:

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

关于ios - 像 NSNumber 这样的 NSString stringWithFormat 的装箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24794816/

相关文章:

iphone - 如何将来自服务器的图像设置为矩形按钮的背景图像

ios - MKMapView 释放内存

iphone - 如何从 iPhone 中的 NSString 中删除空格

ios - 在为 navController 设置动画时保留 UIView

ios - iOS 7 中的媒体查询不起作用

ios - 使用 AVCaptureDevice 时重置 UI 开关

ios - 释放调用顺序

iphone - numberOfLines=2 的 UILabel 使用什么约束高度? (使用 [NSString sizeWithFont...])

iphone - Objective-c 中向字符串添加反斜杠

ios - 使用自动布局时 UITableView 内容大小