Skip to content

Commit 6b78618

Browse files
committed
Avoid crash in release due to superview (second constraint) being nil
1 parent 8bd77ea commit 6b78618

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

Masonry/MASViewConstraint.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,17 @@ - (void)install {
327327
if (!self.firstViewAttribute.isSizeAttribute && !self.secondViewAttribute) {
328328
secondLayoutItem = self.firstViewAttribute.view.superview;
329329
secondLayoutAttribute = firstLayoutAttribute;
330+
331+
if (!secondLayoutItem) { // Don't continue to crash.
332+
// 断言(在 Debug 下生效)
333+
NSString *reason = [NSString stringWithFormat:@"NSLayoutConstraint for %@: A multiplier of 0 or a nil second item together with a location for the first attribute creates an illegal constraint of a location equal to a constant. Location attributes must be specified in pairs.", self.firstViewAttribute.view];
334+
NSAssert(NO, reason);
335+
336+
// 发送通知,让外界处理(在 Debug / Release 下生效)
337+
NSException *exception = [[NSException alloc] initWithName:@"MasonryException" reason:reason userInfo:nil];
338+
[[NSNotificationCenter defaultCenter] postNotificationName:@"AutoLayoutExceptionNotification" object:exception];
339+
return;
340+
}
330341
}
331342

332343
MASLayoutConstraint *layoutConstraint

0 commit comments

Comments
 (0)