HenryGale
HenryGale

Reputation: 681

NSString Formatting Issue

I am having some trouble formatting an NSString

So i have and NSString and an Int

I want to use them to set an Image in a IUImage View, here is my code

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

the goal is for this is to read [UIImage imageNamed:@"test0.png"];

im getting an "expression result unused for the "headOnString" string and an "incompatible pointer conversion sending int to NSString"

I always have trouble with string formatting. Any suggestions would be greatly appreciated. Thanks.

Upvotes: 0

Views: 843

Answers (1)

Tomasz Stanczak
Tomasz Stanczak

Reputation: 13164

use this:

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

and take care that the format fits the type of the variables, you can easily crash by using wrong format for your variables.

Upvotes: 3

Related Questions