通过添加两个NSStrings创建文件名 [英] Creating a file name by adding two NSStrings
问题描述
我正在载入档案。文件名取决于存储在数组中的字符串。在下面的示例中,我想加载tabRed.png。 @Red存储在数组中。
I am trying to load a file. The file name depends on a string stored in an array. In the following example, I would like to load tabRed.png. @"Red" is stored in an array.
这是我试过的:
UIImage *tabImage = [UIImage imageNamed:(@"tab%@.png", [self.currentNoteBook.tabColours objectAtIndex:0])];
但我只有Red作为输出,而不是tabRed.png。我做错了什么?
But I only got "Red" as an output and not "tabRed.png". What am I doing wrong?
推荐答案
UIImage *tabImage = [UIImage imageNamed:[NSString stringWithFormat:@"tab%@.png", [self.currentNoteBook.tabColours objectAtIndex:0]]];
您需要使用 NSString stringWithFormat
。逗号运算符(,
)做了一个不同的事情。
You need to use NSString stringWithFormat
. The comma operator (,
) does quite a different thing.
从维基百科,
在C和C ++编程语言中,逗号运算符(由标记
,
表示)是一个二进制运算符,计算其第一个操作数并丢弃结果,然后计算第二个操作数并返回此值(和类型)。
In the C and C++ programming languages, the comma operator (represented by the token
,
) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).
因此,当您写(@tab%@。png,[self.currentNoteBook.tabColours objectAtIndex :0])
,它实际上丢弃第一个字符串,并返回刚刚为红色的数组中的对象。
So when you write (@"tab%@.png", [self.currentNoteBook.tabColours objectAtIndex:0])
, it actually discards the first string and returns the object in array which is just "Red".
这篇关于通过添加两个NSStrings创建文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!