简体   繁体   English

如何在Swift中为多个变量分配属性?

[英]How do you assign a property to multiple variables in Swift?

I have a switch statement and in each of the cases I have a number of statements which read: 我有一个switch语句,在每种情况下,我都有许多语句如下:

star1.isHidden = true

star1 in this instance is an image view. 在这种情况下,star1是图像视图。 Is there a way of assigning this to multiple variables, such as star2, star3 etc. With 10 cases it amounts to many lines of code and I can't help but think there's a more efficient/readable way of doing it. 有没有一种方法可以将其分配给多个变量,例如star2,star3等。在10种情况下,它相当于许多行代码,我不禁想到有一种更有效/更易理解的方法。

If you are using interface builder, you could use OutletCollection. 如果使用接口构建器,则可以使用OutletCollection。 Otherwise, you could create an array of all the variables and use a for-in loop. 否则,您可以创建所有变量的数组并使用for-in循环。

You can extend a sequence of UIView s that will iterate through the views and set each hidden. 您可以扩展UIView的序列,该序列将遍历视图并设置每个视图。

extension Sequence where Element == UIView {
    func setHidden(_ hide: Bool) {
        forEach { $0.isHidden = hide }
    }
}

let viewsToHide = [star1, star2, star3]
viewsToHide.setHidden(true)

There are many ways, one of the most common would be a for loop. 有很多方法,最常见的一种是for循环。 In this instance you would need a collection of stars such as an array: [Star]() 在这种情况下,您将需要一组星星,例如一个数组: [Star]()

for (star in stars) {
   star.isHidden = true
}

There isn't enough information about your setup to give you a much better recommendation. 没有足够的信息来提供有关您的设置的更好的建议。

If the objects are NSObject-based, you can use key-value coding. 如果对象是基于NSObject的,则可以使用键值编码。 This is in a Swift playground, but it works everywhere: 这是在Swift操场上,但可以在任何地方使用:

class Star:NSObject {
    public var hidden = false
}

let star1 = Star()
let star2 = Star()
let star3 = Star()
let star4 = Star()
let star5 = Star()
let starMap:NSArray = [star1, star2, star3, star4]

starMap.setValue(true, forKey: "hidden")

操场结果

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

相关问题 Swift 4.x / 5.x中是否存在Global变量的多个副本? 如果是这样,您如何停止呢? - Are there multiple copies of Global variables in Swift 4.x/5.x? If so, how do you stop that? 如何使用Swift以编程方式分配IBOutlet? - How do you assign an IBOutlet programmatically using Swift? 如何在Swift中创建具有多个变量的数组 - How Do I Create An Array With Multiple Variables In Swift Swift 3:如何在具有多个标签的tableviewcell中包装内容? - Swift 3 : How do you wrap content in a tableviewcell with multiple labels? 在Swift2 CoreData fetchrequest中如何包含多个实体? - How do you include multiple entites in a Swift2 CoreData fetchrequest? 如何从Firebase数据库检索特定值并将其分配给Swift 4中的特定变量? - How do I retrieve specific values from a firebase database and assign it to specific variables in Swift 4? 为Swift属性赋值 - Assign value to Swift property 如何将变量从Swift传递到PHP以在MySQL查询中使用? - How do you pass variables from Swift into PHP to be used in mySQL queries? 如何在Swift中将具有多个输出的函数的输出分配给不同的变量? - How do I assign outputs of a function with multiple outputs to different varables in Swift? 如何将触摸分配给两个CCColorLayer? - how do you assign touches to two CCColorLayer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM