开发者

Core-Plot: App Crashes if plot points are passed as NSArray of numbers in iPhone SDK

In my iPhone app, I have added core plot.

It works fine if I randomly create values through the for loop (commented lines in code below)

When I try passing an开发者_运维知识库 array of values as the plot points the app crashes.

What could be wrong?

I tried debugging but values are fetched from the array correctly. Also there is no error message in Console when the app crashes.

Below is the code where the array values are passed

Code:

- (void)generateData

  {

     NSArray *A = [[NSArray alloc] initWithObjects:@"1000",@"2000",@"1100",@"4000",nil];

     NSArray *B = [[NSArray alloc] initWithObjects:@"1100",@"2200",@"3300",@"4400",nil];

     if (plotData == nil)
     {

           NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];

           for (NSUInteger i = 0; i < 4; i++) 
           {

                id x = (NSNumber *)[NSNumber numberWithUnsignedInteger:[[A objectAtIndex:i] intValue]];
                id y = (NSNumber *)[NSNumber numberWithUnsignedInteger:[[B objectAtIndex:i] intValue]];
               // id x = [NSDecimalNumber numberWithDouble:1.0 + i * 0.05];
              // id y = [NSDecimalNumber numberWithDouble:1.2 * rand()/(double)RAND_MAX + 0.5];

               [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
           }

           plotData = [contentArray retain];
    }
  }


Before talking about Core Plot, let's clean up the code you've posted:

- (void)generateData
{
    if (plotData == nil)
    {        
        plotData = [[NSMutableArray alloc] initWithCapacity:100];

        // Manually specifying values
//        [plotData addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1000], @"x", [NSNumber numberWithInt:1100], @"y", nil]];
//        [plotData addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:2000], @"x", [NSNumber numberWithInt:2200], @"y", nil]];
//        [plotData addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1100], @"x", [NSNumber numberWithInt:3300], @"y", nil]];
//        [plotData addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:4000], @"x", [NSNumber numberWithInt:4400], @"y", nil]];

        // Looping for values
        for (NSUInteger currentValueIndex = 0; currentValueIndex < 4; currentValueIndex++) 
        {
            [plotData addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1000 * currentValueIndex], @"x", [NSNumber numberWithInt:1000 * currentValueIndex], @"y", nil]];
        }
    }
}

You have two NSArrays that you're leaking, even before the check for plotData being nil. I've removed those, with their function being replaced by the commented-out code where your listed values are manually added to the plotData array. These leaks could potentially lead to an out-of-memory crash if you repeatedly called -generateData.

I've then simplified the for loop for populating the plotData array with some values. There's no need to use an NSMutableDictionary here (even though the Core Plot example application you pasted this line from uses one).

To use this in Core Plot, you'd need to make sure your data source delegate methods look like the following:

-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot 
{
    return [plotData count];
}

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{
    return [[plotData objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")];
}

If your application still crashes with these fixes, your problem is not with the data you're providing to Core Plot, but elsewhere in your application. As always, enable breakpoints on exceptions, run with breakpoints on, and see at what exact line your application crashes. Read the stack traces.


Got the solution.

It was because it was treating the array values as strings so I refered to this link

iphone, using an array to define in core-plot range

and that solved my problem.

Thanks Brad Larson for your help. Keep up your good work.!!

Hope this helps someone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜