核心绘图使用NSArrayController绑定到核心数据崩溃

时间:2013-10-24 16:23:10

标签: core-data core-plot nsarraycontroller

我正在尝试使用核心图和绑定来绘制核心数据中的数据。 我的核心数据模型:

enter image description here

我能够插入,所以我知道我的联系很好......但是这个炸弹在绑定时:

[<NSArrayController 0x6000001c40b0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key measuredate.

-(void)addPlotWithIdentifier:(NSString *)identifier context:(NSManagedObjectContext *)moc
{

    CPTScatterPlot *newPlot = [[CPTScatterPlot alloc] initWithFrame:graph.bounds];
    dataSourceLinePlot.identifier     = identifier;



    CPTMutableLineStyle *lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
    lineStyle.lineWidth              = 2.0;
    lineStyle.lineColor              = [CPTColor greenColor];
    dataSourceLinePlot.dataLineStyle = lineStyle;


    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1380243600)
                                                    length:CPTDecimalFromFloat(1382835600)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
                                                     length:CPTDecimalFromFloat(100)];

    [dataSourceLinePlot setPlotSpace:plotSpace];
    NSLog(@"Create a controller");
    NSArrayController *newController=[[NSArrayController alloc] initWithContent:nil];
    NSLog(@"MOC: set context");
    [newController setManagedObjectContext:moc];
    NSLog(@"MOC: set Entity");
    [newController setEntityName:@"BM_Measure"];
    NSLog(@"MOC: set Editable");
    [newController setEditable:FALSE];
    NSLog(@"MOC: set sort descriptors");
    [newController setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"measuredate" ascending:YES]]];
    NSLog(@"MOC: fetch");

    [newController setAutomaticallyPreparesContent:TRUE];
    [newController setAutomaticallyRearrangesObjects:TRUE];
    NSError *error;
    if ([newController  fetchWithRequest:nil merge:NO error:&error] == NO){
        NSLog(@"Couldnt fetch");
    }
    NSLog(@"%@",newController);

    NSLog(@"Bind x");
    [newPlot bind:CPTScatterPlotBindingXValues toObject:newController withKeyPath:@"measuredate" options:nil];
    NSLog(@"Bind y");
    [newPlot bind:CPTScatterPlotBindingYValues toObject:newController withKeyPath:@"measurevalue" options:nil];
    NSLog(@"Save Objects");

    [g_Plots addObject:newPlot];
    [g_ArrayControllers addObject:newController];
    [graph addPlot:newPlot];
    NSLog(@"Added... %ld",[g_Plots count]);



}

1 个答案:

答案 0 :(得分:0)

Core Plot绑定需要数据数组。这是来自 CPTTestApp 示例应用程序的绑定代码:

[boundLinePlot bind:CPTScatterPlotBindingXValues
           toObject:self 
        withKeyPath:@"arrangedObjects.x"
            options:nil];
[boundLinePlot bind:CPTScatterPlotBindingYValues
           toObject:self
        withKeyPath:@"arrangedObjects.y"
            options:nil];