带间隙的散点图 -[NSNull doubleValue]:发送到实例的无法识别的选择器

ScatterPlot with gap -[NSNull doubleValue]: unrecognized selector sent to instance

我一直在尝试使用以下建议在我的散点图中创建一个间隙: iOS Scatter core plot with a gap

我想通过创建数据来绘制 2 条垂直线:

NSMutableArray<NSDictionary *> *contentArray = [[NSMutableArray alloc] init];

        [contentArray addObject:@{ @"x": _startPointValue, @"y": _startValue }];
        [contentArray addObject:@{ @"x": _startPointValue, @"y": @5 }];

        //add null points not to link the 2 lines
        [contentArray addObject:@{ @"x": _endPointValue, @"y": [NSNull null] }];
        [contentArray addObject:@{ @"x": _endPointValue, @"y": _endValue }];
        [contentArray addObject:@{ @"x": _endPointValue, @"y": @5 }];

        _verticalLinesData = contentArray;

但我收到错误消息:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull doubleValue]: unrecognized selector sent to instance 0x1a0f0e490'

当我注释掉带有 [NSNull null] 的行时,这些行被正确绘制(但连接)。

可能是什么原因。我不在其他地方使用 _verticalLinesData(仅在 coreplot 的数据源方法中)。

编辑: 我的数据源方法:

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
if ( [plot.identifier isEqual:VERTICALLINESPLOTID] ) {
        return _verticalLinesData.count;
    }
    else {
        return 0;
    }
}

-(id)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
    NSString *key = (fieldEnum == CPTScatterPlotFieldX ? @"x" : @"y");
    NSNumber *num = [[NSNumber alloc] init];
   if ([plot.identifier isEqual:VERTICALLINESPLOTID] ) {
        num = _verticalLinesData[index][key];
        num = @([num doubleValue]);
    }
    else {
        num = 0;
    }
    return num;
}

你不应该在字典键值对中使用'[NSNull null]'。

当您尝试将此 null 值转换为 double 值时发生崩溃

所以你有 2 个选择

  1. 避免插入“[NSNull null]”。相反,您可以插入 @(-1); 或
  2. 当你是 double/float 值
  3. 时你可以检查空值

您可以 return 直接来自 _verticalLinesData 的值。删除行 num = @([num doubleValue]); 它将按预期工作。