Asserting

suggest change
@implementation Triangle

...

-(void)setAngles:(NSArray *)angles {
    // Assign the ivar. self.angles = angles would call this setter again.
    _angles = angles;
    
    NSAssert((_angles.count == 3), @"Triangles must have 3 angles. Array '%@' has %i", _angles, (int)_angles.count);
    
    CGFloat angleA = [_angles[0] floatValue];
    CGFloat angleB = [_angles[1] floatValue];
    CGFloat angleC = [_angles[2] floatValue];
    CGFloat sum = (angleA + angleB + angleC);
    NSAssert((sum == M_PI), @"Triangles' angles must add up to pi radians (180°). This triangle's angles add up to %f radians (%f°)", (float)sum, (float)(sum * (180.0f / M_PI)));
}

These assertions make sure that you don’t give a triangle incorrect angles, by throwing an exception if you do. If they didn’t throw an exception, then the triangle, not being a true triangle at all, might cause bugs in later code.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents