Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C - incompatible integer to pointer conversion

I am new to iOS programming.

I am getting an "Incompatible integer to pointer conversion sending 'int' to parameter of type 'NSString *'" error when I try and run the following code:

- (IBAction)oneToSix {
    int rNumber = arc4random() % 6;
    [result setImage:[UIImage imageNamed: rNumber]];
}
like image 975
gadgetmo Avatar asked Feb 02 '26 03:02

gadgetmo


2 Answers

You need to convert the int to a NSString before sending it to the imageNamed method. This is because imageNamed takes a NSString as the argument and not an int. Try this:

- (IBAction)oneToSix {
    int rNumber = arc4random() % 6;
    [result setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d", rNumber]]];
}

More info here: UIImage Class Reference.

like image 66
chown Avatar answered Feb 04 '26 18:02

chown


The problem is that imageNamed: takes an NSString as input, not an int. To fix this, just convert the int into an NSString by

[NSString stringWithFormat:@"%d",rNumber]
like image 39
PengOne Avatar answered Feb 04 '26 19:02

PengOne