[英]Find object in array in Swift 2.1
好的,因此Swift在某些方面非常出色,而在某些方面卻不那么出色。
我試圖根據另一個數組中的數組在一個數組中找到一個元素。
我的應用程序中有一些視覺元素,有時需要隱藏和顯示,而不是編寫手動邏輯來顯示和隱藏視覺元素,而是將它們全部放入一個數組中,並調用一個函數,該函數需要一個包含我要隱藏的元素。
例如:我有10個按鈕(或混合使用不同的對象,例如UIImageViews
和UILabels
等),我們可以將它們稱為B1至B10。
如果某個時候我需要隱藏除B3,B4,B7,B9和B10之外的所有元素,我只需調用hideAllExcept(ignore: Array<AnyObject>)(}
,然后func即可處理其余的所有元素。
有時隱藏和顯示哪些元素可能是完全隨機的,因此此方法可能非常強大。
但是,當嘗試檢查第一個數組中的元素是否包含在第二個數組中時,出現以下錯誤: Cannot invoke 'contains' with an argument list of type '(anObject: AnyObject)
如何在Swift 2.1中達到這種效果?
我目前的嘗試如下所示(顯然無法正常工作):
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
}
}
}
}
我不相信Swift數組的contains方法會那樣工作。 您正在調用的contains方法采用一個將為數組中的每個元素調用的塊,如果元素匹配, Bool
您決定是否返回Bool
。
您考慮過使用Set
嗎? 給定Set
上可用的屬性和功能,這似乎是一種更簡單的方法。 請注意,您必須將其聲明為采用Hashable
協議的類型,但是如果您想隱藏元素,則似乎無論如何都希望使用UIView
作為類型( hidden
在UIView
上聲明)。
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
}
}
}
您可能還希望將ignoreElements
元素設置為不隱藏,除非您在其他地方這樣做。 您可以輕松地做到這一點:
for element in ignoreElements {
element.hidden = false
}
首先,您應該使用NSObject
而不是AnyObject
。 喜歡:
var elements = Array<NSObject>()
在您的情況下,我認為使用Set
會更好。 這是代碼段:
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.