问题描述
我正在载入档案。文件名取决于存储在数组中的字符串。在下面的示例中,我想加载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.
从,
因此,当您写(@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创建文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!