I am adding a scatter plot to my app (iGear) so when the user selects one, two or three chainrings combined with a cogset on a bike, lines will show the gears meters.
The problem is that Core Plot only shows the plots when three chainrings are selected. I need your help, this is my first try at Core Plot and I'm lost.
My code is the following:
iGearMainViewController.m
- (IBAction)showScatterIpad:(id)sender {
cogsetToPass = [NSMutableArray new];
arrayForChainringOne = [NSMutableArray new];
arrayForChainringTwo = [NSMutableArray new];
arrayForChainringThree = [NSMutableArray new];
//behavior according to number of chainrings
switch (self.segmentedControl.selectedSegmentIndex) {
case 0: // one chainring selected
for (int i = 1; i<= [cassette.numCogs intValue]; i++) {
if (i <10) {
corona = [NSString stringWithFormat:@"cog0%d",i];
}else {
corona = [NSString stringWithFormat:@"cog%d",i];
}
float one = (wheelSize*[_oneChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
float teeth = [[cassette valueForKey:corona] floatValue];
[cogsetToPass addObject:[NSNumber numberWithFloat:teeth]];
[arrayForChainringOne addObject:[NSNumber numberWithFloat:one]];
}
break;
case 1: // two chainrings selected
for (int i = 1; i<= [cassette.numCogs intValue]; i++) {
if (i <10) {
corona = [NSString stringWithFormat:@"cog0%d",i];
}else {
corona = [NSString stringWithFormat:@"cog%d",i];
}
float one = (wheelSize*[_oneChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
//NSLog(@" gearsForOneChainring = %@",[NSNumber numberWithFloat:one]);
float two = (wheelSize*[_twoChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
[cogsetToPass addObject:[NSNumber numberWithFloat:[[cassette valueForKey:corona]floatValue]]];
[arrayForChainringOne addObject:[NSNumber numberWithFloat:one]];
[arrayForChainringTwo addObject:[NSNumber numberWithFloat:two]];
}
break;
case 2: // three chainrings selected
for (int i = 1; i<= [cassette.numCogs intValue]; i++) {
if (i <10) {
corona = [NSString stringWithFormat:@"cog0%d",i];
}else {
corona = [NSString stringWithFormat:@"cog%d",i];
}
float one = (wheelSize*[_oneChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
float two = (wheelSize*[_twoChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
float three = (wheelSize*[_threeChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
[cogsetToPass addObject:[cassette valueForKey:corona]];
[arrayForChainringOne addObject:[NSNumber numberWithFloat:one]];
[arrayForChainringTwo addObject:[NSNumber numberWithFloat:two]];
[arrayForChainringThree addObject:[NSNumber numberWithFloat:three]];
}
default:
break;
}
ScatterIpadViewController *sivc = [[ScatterIpadViewController alloc]initWithNibName: @"ScatterIpadViewController" bundle:nil];
[sivc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
sivc.records = [cassetteNumCogs integerValue];
sivc.cogsetSelected = self.cogsetToPass;
sivc.chainringOne = self.arrayForChainringOne;
sivc.chainringThree = self.arrayForChainringThree;
sivc.chainringTwo = self.arrayForChainringTwo;
[self presentViewController:sivc animated:YES completion:nil];
}
And the child view with the code to draw the plots:
ScatterIpadViewController.m
#pragma mark - CPTPlotDataSource methods
- (NSUInteger)numberOfRecordsForPlot: (CPTPlot *)plot {
return records;
}
- (NSNumber *)numberForPlot: (CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
switch (fieldEnum)
{
case CPTScatterPlotFieldX:
return [NSNumber numberWithInt:index];
break;
case CPTScatterPlotFieldY:{
if ([plot.identifier isEqual:@"one"]==YES) {
//NSLog(@"chainringOne objectAtIndex:index = %@", [chainringOne objectAtIndex:index]);
return [chainringOne objectAtIndex:index];
}else if ([plot.identifier isEqual:@"two"] == YES ){
//NSLog(@"chainringTwo objectAtIndex:index = %@", [chainringTwo objectAtIndex:index]);
return [chainringTwo objectAtIndex:index];
}else if ([plot.identifier isEqual:@"three"] == YES){
//NSLog(@"chainringThree objectAtIndex:index = %@", [chainringThree objectAtIndex:index]);
return [chainringThree objectAtIndex:index];
}
default:
break;
}
}
return nil;
}
The error returned is an exception on trying to access an empty array.
2012-11-15 11:02:42.962 iGearScatter[3283:11603] Terminating app due to uncaught exception 'NSRangeException', reason: ' -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
First throw call stack:
(0x1989012 0x1696e7e 0x192b0b4 0x166cd 0x183f4 0x1bd39 0x179c0 0x194fb 0x199e1 0x43250 0x14b66 0x13ef0 0x13e89 0x3b5753 0x3b5b2f 0x3b5d54 0x3c35c9 0x5c0814 0x392594 0x39221c 0x394563 0x3103b6 0x310554 0x1e87d8 0x27b3014 0x27a37d5 0x192faf5 0x192ef44 0x192ee1b 0x29ea7e3 0x29ea668 0x2d265c 0x22dd 0x2205 0x1)*
libc++abi.dylib: terminate called throwing an exception
Thank you!