抱歉,如果这个问题太模糊,但是我真的不明白发生了什么。
我有一个集成了crashlytics lib的应用程序。而且我有一些奇怪的崩溃。这是日志:

102行是

[path addArcWithCenter:CGPointMake(kLeftHorizontalMargin + xScale*i/2, kVerticalTopMargin + (maxValue - value)*yScale) radius:kPathLineWidth startAngle:0 endAngle:2*M_PI clockwise:YES];

还有一点:

线程:崩溃:com.apple.main-thread
0  libsystem_kernel.dylib         0x3846f1fc __pthread_kill + 8
1  libsystem_pthread.dylib        0x384d6a53 pthread_kill + 58
2  libsystem_c.dylib              0x3842002d abort + 76
3  libsystem_c.dylib              0x383ffc6b __assert_rtn + 182
4  CoreGraphics                   0x2dc60563 CGPathAddArc + 186
5  UIKit                          0x30589ed7 -[UIBezierPath addArcWithCenter:radius:startAngle:endAngle:clockwise:] + 70
6  Exchange                       0x00010d25 -[GraphView drawRect:] (GraphView.m:102)
7  UIKit                          0x303cb749 -[UIView(CALayerDelegate) drawLayer:inContext:] + 372
8  QuartzCore                     0x30002049 -[CALayer drawInContext:] + 100
9  QuartzCore                     0x2ffeb813 CABackingStoreUpdate_ + 1858
10 QuartzCore                     0x300c5735 ___ZN2CA5Layer8display_Ev_block_invoke + 52
11 QuartzCore                     0x2ffeb0c3 x_blame_allocations + 82
12 QuartzCore                     0x2ffead77 CA::Layer::display_() + 1118
13 QuartzCore                     0x2ffce969 CA::Layer::display_if_needed(CA::Transaction*) + 208
14 QuartzCore                     0x2ffce601 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 24
15 QuartzCore                     0x2ffce00d CA::Context::commit_transaction(CA::Transaction*) + 228
16 QuartzCore                     0x2ffcde1f CA::Transaction::commit() + 314
17 QuartzCore                     0x2ffc7b4d CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 56
18 CoreFoundation                 0x2db90f71 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
19 CoreFoundation                 0x2db8e8ff __CFRunLoopDoObservers + 286
20 CoreFoundation                 0x2db8ec4b __CFRunLoopRun + 738
21 CoreFoundation                 0x2daf9541 CFRunLoopRunSpecific + 524
22 CoreFoundation                 0x2daf9323 CFRunLoopRunInMode + 106
23 GraphicsServices               0x328302eb GSEventRunModal + 138
24 UIKit                          0x303b01e5 UIApplicationMain + 1136
25 Exchange                       0x00022507 main (main.m:16)

这是我的drawRect::
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    if (self.datesAndValues.count == 0)
        return;

    double xScale = (self.frame.size.width - kLeftHorizontalMargin - kRightHorizontalMargin)/((self.datesAndValues.count - 1)/2);
    double yScale;
    if (maxValue == minValue)
        yScale = 1;
    else
        yScale = (self.frame.size.height - kVerticalTopMargin - kVerticalBottomMargin)/(maxValue - minValue);

    [self drawWeekSeparatorsInRect:rect
                        withXScale:xScale];

    UIFont* bigFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:kMinMaxLabelFontSize];
    UIFont* smallFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:kLocalMinMaxLabelFontSize];

    UIBezierPath* path = [UIBezierPath bezierPath];

    for (NSInteger i=0; i<self.datesAndValues.count; i+=2)
    {
        double value = [self.datesAndValues[i+1] doubleValue];
        if (i == 0)
            [path moveToPoint:CGPointMake(kLeftHorizontalMargin, kVerticalTopMargin + (maxValue - value)*yScale)];
        else
            [path addLineToPoint:CGPointMake(kLeftHorizontalMargin + xScale*i/2, kVerticalTopMargin + (maxValue - value)*yScale)];

        if (i == self.datesAndValues.count - 2)
            [path addArcWithCenter:CGPointMake(kLeftHorizontalMargin + xScale*i/2, kVerticalTopMargin + (maxValue - value)*yScale) radius:kPathLineWidth startAngle:0 endAngle:2*M_PI clockwise:YES];

        NSDate* date = self.datesAndValues[i];
        if ([self.datesOfMax containsObject:date] &&
            value != maxValue)
        {
            UIColor* textColor = [UIColor greenColor];
            NSDictionary* stringAttrs = @{ NSFontAttributeName : smallFont,
                                           NSForegroundColorAttributeName : textColor };
            NSString* maxValueString = [NSString stringWithFormat:@"%.2f", value];
            NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:maxValueString
                                                                          attributes:stringAttrs];
            CGFloat x = kLeftHorizontalMargin + xScale*i/2;
            CGRect textRect = [maxValueString boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)
                                                           options:NSStringDrawingUsesLineFragmentOrigin
                                                        attributes:stringAttrs
                                                           context:nil];
            if (x + textRect.size.width + kLeftHorizontalMargin > self.frame.size.width)
                x = self.frame.size.width - textRect.size.width - kLeftHorizontalMargin;
            [attrStr drawAtPoint:CGPointMake(x, (maxValue - value)*yScale)];
        }

        if (value == maxValue)
        {
            UIColor* textColor = [UIColor greenColor];
            NSDictionary* stringAttrs = @{ NSFontAttributeName : bigFont,
                                           NSForegroundColorAttributeName : textColor };
            NSString* maxValueString = [NSString stringWithFormat:@"%.2f", value];
            NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:maxValueString
                                                                          attributes:stringAttrs];
            CGRect textRect = [maxValueString boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)
                                                           options:NSStringDrawingUsesLineFragmentOrigin
                                                        attributes:stringAttrs
                                                            context:nil];
            CGFloat x = kLeftHorizontalMargin + xScale*i/2 - textRect.size.width;
            if (x < kLeftHorizontalMargin)
                x = kLeftHorizontalMargin;
            [attrStr drawAtPoint:CGPointMake(x, kMaxValueVerticalTopMargin + (maxValue - value)*yScale)];
        }

        if ([self.datesOfMin containsObject:date] &&
            value != minValue)
        {
            UIColor* textColor = [UIColor redColor];
            NSDictionary* stringAttrs = @{ NSFontAttributeName : smallFont,
                                           NSForegroundColorAttributeName : textColor };
            NSString* minValueString = [NSString stringWithFormat:@"%.2f", value];
            NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:minValueString
                                                                          attributes:stringAttrs];
            CGFloat x = kLeftHorizontalMargin + xScale*i/2;

            [attrStr drawAtPoint:CGPointMake(x, kVerticalTopMargin + (maxValue - value)*yScale)];
        }
        if (value == minValue)
        {
            UIColor* textColor = [UIColor redColor];
            NSDictionary* stringAttrs = @{ NSFontAttributeName : bigFont,
                                           NSForegroundColorAttributeName : textColor };
            NSString* minValueString = [NSString stringWithFormat:@"%.2f", value];
            NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:minValueString
                                                                          attributes:stringAttrs];
            CGFloat x = kLeftHorizontalMargin + xScale*i/2;
            CGRect textRect = [minValueString boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)
                                                           options:NSStringDrawingUsesLineFragmentOrigin
                                                        attributes:stringAttrs
                                                           context:nil];
            if (x + textRect.size.width + kLeftHorizontalMargin > self.frame.size.width)
                x = self.frame.size.width - textRect.size.width - kLeftHorizontalMargin;
            [attrStr drawAtPoint:CGPointMake(x, kVerticalTopMargin + (maxValue - value)*yScale)];
        }
    }
    path.lineWidth = kPathLineWidth;
    [[UIColor lightGrayColor] setStroke];
    [path stroke];
}

最佳答案

如果self.datesAndValues中只有一对日期/值对,则代码会崩溃。
看这行:

double xScale = (self.frame.size.width - kLeftHorizontalMargin - kRightHorizontalMargin)/((self.datesAndValues.count - 1)/2);

如果self.datesAndValues的计数为2,则xScale的结果值为+Inf,因为((self.datesAndValues.count - 1)/2)将为0。

关于ios - drawRect中的SIGABRT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24568098/

10-13 04:37