0

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. 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?

2 Answers 2

6

[UIImage imageNamed:[NSString stringWithFormat:@"cover_%d.jpg", value]];

The string literal syntax doesn't support formats.

Sign up to request clarification or add additional context in comments.

Comments

2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.