You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You will end up returning 3 duplicate copies of myLabel.
Explanation
"* UILabel" means any UILabel, or any descendent of a UIView that is a UILabel. myLabel matches this condition on three separate occasions:
myLabel is a UILabel that has descended from myView, which is a UIView
myLabel is a UILabel that has descended from myScrollView, which is a UIView
myLabel is a UILabel
Walkthrough
# Evaluation algorithm for finding views by classdeffind(view, class):
res= []
ifview.matches(class):
res.add(view)
forsubviewinview.subviews:
res.addAll(find(subview, class))
# query string: "* UILabel" => "UIView UILabel" # So we are searching for all UIViews, then from those results, # we check if any result is a UILabel, recurse on the subviews, # and return cumulation of all UILabel results. find(mainWindow, UIView) => [ myView, myScrollView, myLabel ] #returns the whole heirarchy #Then we recurse on each match, but searching for UILabelfind(myView, UILabel) => [ myLabel ]
find(myScrollView, UILabel) => [ myLabel ]
find(myLabel, UILabel) => [ myLabel ]
#Final return value:
[ myLabel, myLabel, myLabel ]
This can be easily resolved if we use an NSMutableOrderedSet to gather results instead of an NSMutableArray.
The text was updated successfully, but these errors were encountered:
sapieneptus
changed the title
Fix Duplicate Query Results: Use NSSet instead of NSArray
Fix Duplicate Query Results: Use NSOrderedSet instead of NSArray
Jan 7, 2016
Motivation
Suppose you have a hierarchy:
And you do a query
You will end up returning 3 duplicate copies of
myLabel
.Explanation
"* UILabel"
means any UILabel, or any descendent of a UIView that is a UILabel.myLabel
matches this condition on three separate occasions:myLabel
is a UILabel that has descended frommyView
, which is a UIViewmyLabel
is a UILabel that has descended frommyScrollView
, which is a UIViewmyLabel
is a UILabelWalkthrough
This can be easily resolved if we use an
NSMutableOrderedSet
to gather results instead of anNSMutableArray
.The text was updated successfully, but these errors were encountered: