IOS face Question (a): Looking for a recent public View--from Tang Qiao

Source: Internet
Author: User

Title: Find the nearest public View of two UIView, and if not present, output nil.

Analysis: This is actually the problem of finding the nearest public ancestor in the data structure.

The UIViewController relationship between all the view in one can actually be regarded as a tree, UIViewController the view variable is the root node of the tree, and the other view is the direct or indirect child node of the root node.

So we can always find the root node through the view's Superview property. It is important to note that in code, we also need to consider various illegal inputs, if you enter nil, you also need to deal with, avoid exceptions. Here is the path code that finds the specified view to root view:

+ (Nsarray *) Superviews: (UIView *) View {    if (view = = Nil)        {return  @[];    }     *result = [Nsmutablearray array];      while (View! = Nil) {        [result addobject:view]        ; = view.superview;    }     return [result copy];}

Then for two view a and view B, we can get two paths, and we're looking for the closest public node in the subject.

A straightforward approach: take all the nodes in the first path and go to the second node to find them. Assuming that the average length of the path is n, because each node is to find n times, there are a total of n nodes, so the time complexity of this method is O (n^2).

+ (UIView *) Commonview_1: (UIView *) Viewa Andview: (UIView *) viewb {Nsarray*ARR1 =[self Superviews:viewa]; Nsarray*ARR2 =[self SUPERVIEWS:VIEWB];  for(Nsuinteger i =0; i < Arr1.count; ++i) {UIView*targetview =Arr1[i];  for(Nsuinteger j =0; J < Arr2.count; ++j) {if(TargetView = =Arr2[j]) {                returnTargetView; }        }    }    returnNil;}

An improved approach: We put all the points in a path first into the Nsset. Because Nsset's internal implementation is a hash table, the time complexity of finding elements becomes O (1), we have N nodes altogether, so the total time complexity is optimized to O (N).

 + (UIView *) Commonview_2: (UIView *) Viewa Andview: (UIView * *a    RR1 = [self Superviews:viewa];    Nsarray  *arr2 = [self SUPERVIEWS:VIEWB];    Nsset  *set  = [Nsset SETWITHARRAY:ARR2];  for  (Nsuinteger i = 0 ; i < Arr1.count;        ++i) {UIView  *targetview = Arr1[i];  if  ([set   Containsobject:targetview])        { return   TargetView; }}  return   

In addition to using Nsset, we can also use the idea of merging sort, with two "pointers", pointing to the root node of two paths, and then starting from the root node, looking for the first different node, the first public node of a different node, is our answer. The code is as follows:

/*O (N) Solution*/+ (UIView *) Commonview_3: (UIView *) Viewa Andview: (UIView *) viewb {Nsarray*ARR1 =[self Superviews:viewa]; Nsarray*ARR2 =[self SUPERVIEWS:VIEWB]; Nsinteger P1= Arr1.count-1; Nsinteger P2= Arr2.count-1; UIView*answer =Nil;  while(P1 >=0&& P2 >=0) {        if(ARR1[P1] = =ARR2[P2]) {Answer=ARR1[P1]; } p1--; P2--; }    returnanswer;}

We can also use UIView's IsDescendant method to simplify our code, but in this case, the time complexity should also be O (n^2). Lexrus provides the following Swift version of the code:

/// without FlatMap extension UIView {    --UIView?  {            if let S = superview {                   if  view.isdescendant (of:s) {                                   Return  s            else  {                                 return  S.commonsuperview (of:view }}} "            return nil}}"                    

In particular, if we use the Optinal FlatMap method, we can simplify the above code to a shorter, basically a line of code to get it done. How, have you learned it?

extension UIView {    --UIView?  {        return  superview.flatmap {            view.isdescendant (of: $0)?               $0 : $0. Commonsuperview (of:view)     }}}

IOS face Question (a): Looking for a recent public View--from Tang Qiao

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.