Objective-C Language Asserting


@implemenetation Triangle


-(void)setAngles:(NSArray *)_angles {
    self.angles = _angles;
    NSAssert((self.angles.count == 3), @"Triangles must have 3 angles. Array '%@' has %i", self.angles, (int)self.angles.count);
    CGFloat angleA = [self.angles[0] floatValue];
    CGFloat angleB = [self.angles[1] floatValue];
    CGFloat angleC = [self.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 than the triangle, not being a true triangle at all, might cause some bugs in later code.