Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

incompatible pointer to integer conversion assigning to 'NSInteger' (aka 'int')

Using a UISegmentedControl, I am getting the error suggested in the Title for the last line of this code.

- (IBAction)segmentAction:(id)sender
{
    //NSLog(@"segmentAction: selected segment = %d", [sender selectedSegmentIndex]);
    NSArray *speeds = @[@1.25, @1.5, @2.0];
    speed = [speeds objectAtIndex:[sender selectedSegmentIndex]];
}

The declaration for speed is NSInteger speed;.

Can you help with the raised issue, please?

like image 699
zerowords Avatar asked Jun 22 '13 15:06

zerowords


1 Answers

You're assigning a NSNumber * to a NSInteger.

Keep in mind that NSArrays store objects and that @1.25 is a shorthand for [NSNumber numberWithFloat:1.25]

Change it to

speed = [[speeds objectAtIndex:[sender selectedSegmentIndex]] integerValue];

or with a nicer syntax

speed = speeds[sender.selectedSegmentIndex].integerValue;

Also I think what you want is speed to be a float, instead of a NSInteger. You cannot assign 1.25, for instance, to a NSInteger.

like image 59
Gabriele Petronella Avatar answered Sep 30 '22 00:09

Gabriele Petronella