NSString 格式问题

Posted

技术标签:

【中文标题】NSString 格式问题【英文标题】:NSString Formatting Issue 【发布时间】:2011-05-01 07:28:00 【问题描述】:

我在格式化 NSString 时遇到了一些问题

所以我有 NSString 和一个 Int

我想用它们在 IUImage 视图中设置图像,这是我的代码

headOnString = @"test";
slideNumber = 0;
NSLog(@"%@%i.png",headOnString, slideNumber);
photoDisplay.image = [UIImage imageNamed:(@"%@%i.png", headOnString, slideNumber)];

目标是读取 [UIImage imageNamed:@"test0.png"];

我得到一个“headOnString”字符串未使用的“表达式结果” 以及“将 int 发送到 NSString 的不兼容指针转换”

我总是遇到字符串格式化的问题。任何建议将不胜感激。 谢谢。

【问题讨论】:

另见:***.com/questions/5846858/… 【参考方案1】:

使用这个:

photoDisplay.image = [UIImage imageNamed: [NSString stringWithFormat:@"%@%i.png", headOnString, slideNumber]];

并注意格式适合变量的类型,如果为变量使用错误的格式,您很容易崩溃。

【讨论】:

以上是关于NSString 格式问题的主要内容,如果未能解决你的问题,请参考以下文章

NSString 格式问题自动追加 \x

如何将 NSString 格式化为 URL?

以 00:00:00 时钟格式将 NSTimeInterval 转换为 NSString

将 JSON 格式的 NSString 转换为 NSArray 失败

UIApplication openUrl 不适用于格式化的 NSString

将浮点数格式化为 NSString 时截断多余的零