From 12fef0264ec358629a76a50f43f70d2554a98b4a Mon Sep 17 00:00:00 2001 From: wujingpeng Date: Tue, 19 Mar 2019 14:39:27 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=20refactor:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. fix wrong word \`heirachy\` -> \`hierarchy\` 2. Optimization method implementation \`mas_closestCommonSuperview\` (list this https://leetcode.com/problems/intersection-of-two-linked-lists/) --- Masonry/View+MASAdditions.m | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/Masonry/View+MASAdditions.m b/Masonry/View+MASAdditions.m index b99316ad..48a5cb7e 100644 --- a/Masonry/View+MASAdditions.m +++ b/Masonry/View+MASAdditions.m @@ -184,23 +184,20 @@ - (void)setMas_key:(id)key { objc_setAssociatedObject(self, @selector(mas_key), key, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -#pragma mark - heirachy +#pragma mark - hierarchy - (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view { - MAS_VIEW *closestCommonSuperview = nil; - + if (!view) { + return nil; + } + MAS_VIEW *firstViewSuperview = self; MAS_VIEW *secondViewSuperview = view; - while (!closestCommonSuperview && secondViewSuperview) { - MAS_VIEW *firstViewSuperview = self; - while (!closestCommonSuperview && firstViewSuperview) { - if (secondViewSuperview == firstViewSuperview) { - closestCommonSuperview = secondViewSuperview; - } - firstViewSuperview = firstViewSuperview.superview; - } - secondViewSuperview = secondViewSuperview.superview; + + while (firstViewSuperview != secondViewSuperview) { + firstViewSuperview = firstViewSuperview == nil ? view : firstViewSuperview.superview; + secondViewSuperview = secondViewSuperview == nil ? self : secondViewSuperview.superview; } - return closestCommonSuperview; + return firstViewSuperview; } @end