简体   繁体   English

在Swift 2.1中查找数组中的对象

[英]Find object in array in Swift 2.1

Ok so Swift is great in may areas, and not so great in some. 好的,因此Swift在某些方面非常出色,而在某些方面却不那么出色。

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 UIImageViews and UILabels etc), we can call them B1 though B10. 例如:我有10个按钮(或混合使用不同的对象,例如UIImageViewsUILabels等),我们可以将它们称为B1至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. 如果某个时候我需要隐藏除B3,B4,B7,B9和B10之外的所有元素,我只需调用hideAllExcept(ignore: Array<AnyObject>)(} ,然后func即可处理其余的所有元素。

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) 但是,当尝试检查第一个数组中的元素是否包含在第二个数组中时,出现以下错误: Cannot invoke 'contains' with an argument list of type '(anObject: AnyObject)

How can I achieve this effect in Swift 2.1? 如何在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
            }
        }
    }
}

I do not believe the contains method of Swift arrays works like that. 我不相信Swift数组的contains方法会那样工作。 The contains method you're calling takes a block that will be invoked for every element in the array, and it's up to you to return a Bool if the element matches. 您正在调用的contains方法采用一个将为数组中的每个元素调用的块,如果元素匹配, Bool您决定是否返回Bool

Have you considered using a Set ? 您考虑过使用Set吗? It seems like an easier approach given the properties and functions available on a Set . 给定Set上可用的属性和功能,这似乎是一种更简单的方法。 Note that you will have to declare it of a type that adopts the Hashable protocol, but it seems like you'd want to use UIView as your type anyhow if you want to hide your elements ( hidden is declared on UIView ). 请注意,您必须将其声明为采用Hashable协议的类型,但是如果您想隐藏元素,则似乎无论如何都希望使用UIView作为类型( hiddenUIView上声明)。

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 = Set<UIView>()

    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: Set<UIView>){
        for element in elements.subtract(ignoreElements) {
            element.hidden = true
        }
    }
}

You may also want to set elements in ignoreElements to not be hidden, unless you do that somewhere else. 您可能还希望将ignoreElements元素设置为不隐藏,除非您在其他地方这样做。 You could easily do that with: 您可以轻松地做到这一点:

for element in ignoreElements {
    element.hidden = false
}

First of all, you should use NSObject instead of AnyObject . 首先,您应该使用NSObject而不是AnyObject like: 喜欢:

var elements = Array<NSObject>()

In your case, i think use Set would be better. 在您的情况下,我认为使用Set会更好。 here's the snippet: 这是代码段:

import UIKit

class myCollectionOfThings : UIView {

    typealias ButtonSet = Set<UIButton>   // More readability for collections

    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 buttons = ButtonSet()

    func prep(parent: UIView){
        buttons = [B1, B2, B3, B4, B5, B6, B7, B8, B9, B10]

        //I will be setting up all my UIButtons by code here...
    }

    func hideAllExcept(ignoreElements: ButtonSet) {
        let exceptions = buttons.subtract(ignoreElements)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM