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?
You're assigning a NSNumber *
to a NSInteger
.
Keep in mind that NSArray
s 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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With