I'm pretty new to Objective-C, switching over from C++, so excuse my stupid question.
I know with NSLogs
and what not NSLog(@"%d is the index",i);
is valid syntax开发者_Go百科. What is the reason that this: return [UIImage imageNamed:@"cover_%d.jpg", value];
isn't?
I get an error in my IDE telling me that I have too many arguments, i.e. the integer is never being used. How do I get the integer to reflect in a non-log situation?
[UIImage imageNamed:[NSString stringWithFormat:@"cover_%d.jpg", value]];
The string literal syntax doesn't support formats.
The %d (etc.) syntax is a specific convention used by particular functions like NSLog, it's not a general purpose Objective-C operation that you can expect to work with arbitrary method invocations.
Fortunately, another method that supports the %-substitutions is NSSTRing -stringWithFormat:
. You use that method to create a string using the substitutions, then use that string as desired. For example:
NSString *imgName = [NSString stringWithFormat:@"cover_%d.jpg", value];
return [UIImage imageNamed:imgName];
(you could also combine these two lines into a single expression, eliminating the temporary imgName variable)
精彩评论