Ok so Swift is great in may areas, and not so great in some.
I am trying to find an element in one array, based on the array from another array.
I have a few visual elements in my app that I need to hide and show sometimes, and instead of writing manual logic to show and hide the visual elements, I instead put them all in an array and call a function needs a reference array which contains the elements that I want to hide.
For example: I have 10 buttons (or a mix of different objects, such as UIImageViewsand UILabelsetc), we can call them B1 though B10.
If I at some point need to hide ALL elements EXCEPT B3, B4, B7, B9 and B10,
I simply call hideAllExcept(ignore: Array<AnyObject>)(}
and the func handles the rest.
Which elements that are hidden and shown can be completely random at times, so this approach can be very powerful.
However, when trying to check if an element from the first array contains in the second array I get the following error:
Cannot invoke 'contains' with an argument list of type '(anObject: AnyObject)
How can I achieve this effect in Swift 2.1?
My current attempt looks like this (not working, obviously):
class myCollectionOfThings : UIView {
let B1 = UIButton(type: UIButtonType.Custom)
let B2 = UIButton(type: UIButtonType.Custom)
let B3 = UIButton(type: UIButtonType.Custom)
let B4 = UIButton(type: UIButtonType.Custom)
let B5 = UIButton(type: UIButtonType.Custom)
let B6 = UIButton(type: UIButtonType.Custom)
let B7 = UIButton(type: UIButtonType.Custom)
let B8 = UIButton(type: UIButtonType.Custom)
let B9 = UIButton(type: UIButtonType.Custom)
let B10 = UIButton(type: UIButtonType.Custom)
var elements = Array<AnyObject>()
func prep(parent: UIView){
elements = [B1, B2, B3, B4, B5, B6, B7, B8, B9, B10]
//I will be setting up all my UIButtons by code here...
}
func hideAllExcept(ignoreElements: Array<AnyObject>){
var hide = Bool()
for i in 0...ignoreElements.count - 1{
if elements.contains(ignoreElements[i]){ //I get the error here
//Hide the element here
}
}
}
}
UIButtoninstances, why do you declareelementsasAnyObject?UIButton.UILabel,UIImageViewandUIButtonare all subclasses ofUIView, soUIViewlike in your answer is much better thanAnyObjectUIButtontype.