[Swift [如何迭代对象的所有属性

时间:2022-03-08 19:28:25

I have an object defined with about 15 properties. I am trying to iterate over all of the properties that aren't equal to zero and are of type Int or Double. Something like this:

我有一个约15个属性定义的对象。我试图迭代所有不等于零的属性,并且类型为Int或Double。像这样的东西:

/*
object.price1 = 10.0
object.price2 = 9.9
object.price3 = 8.9
object.price4 = 10.1
object.name = "banana"
object.type = "fruit"
object.quantitySold = 0
object.dateIntroduced = ""
*/

let banana = object()

for property in banana {
    object.property = property*2
}

Any ideas on how to accomplish this?

有关如何实现这一目标的任何想法?

2 个解决方案

#1


0  

This isn't an easy thing to do in Swift (although possible), but that's no bad thing: being able to iterate over an object's properties and mutate (change) them without having to directly reference a property by name could easily lead to some confusion for you or another developer later on, when you're trying to find out why a property of an object has changed.

这在Swift中并不是一件容易的事情(尽管可能),但这并不是坏事:能够迭代对象的属性并变异(更改)它们而不必直接引用属性名称可能很容易导致一些当您试图找出对象的属性发生变化的原因时,您或其他开发人员稍后会感到困惑。

Much better instead to make this kind of operation explicit, and name it correctly. Something like the following:

更好的是将这种操作显式化,并正确命名。类似于以下内容:

extension Object {
    func doubledPrice() -> Object {
        return Object(
            price1: price1 * 2,
            price2: price2 * 2,
            price3: price3 * 2,
            price4: price4 * 2,
            name: name, //we can't double a string
            type: type,
            quantitySold: quantitySold, //I've named the func assuming you won't double the quantitySold, obviously if that's not the desired behaviour then this needs to change
            dateIntroduced: dateIntroduced //can't double a date
        )
    }
}

#2


0  

Make the prices an array? This is from my phone so check for errors. The bad side to this is how messy it can be and how difficult it would be to keep organized.

把价格变成一个数组?这是来自我的手机,所以检查错误。这方面的不好之处在于它是多么混乱以及保持井井有条的难度。

 class MyProduct{
 var price1 : Int
 var price2 : Int
 var price3 : Int

  var pricesArray : [Int]

  init(price1 : Int, price2 : Int, price3 : Int, pricesArray : [Int]){
  self.price1 = price1
  self.price2 = price2
  self.price3 = price3


 for i in 0...2
  { pricesArray.append(0)}
  pircesArray[0] = price1
 pricesArray[1] = price2
  pricesArray[2] = price3


self.pricesArray = pricesArray
 }_

 //then to go over it like
    for i in 0...3{
   banana.pricesArray[i] = banana.procesArray[i] * 2
   }

Or you could make a function in the product class

或者您可以在产品类中创建一个函数

  func equate( sum : Int)
  {
  yourVar = yourVar * sum
  }

#1


0  

This isn't an easy thing to do in Swift (although possible), but that's no bad thing: being able to iterate over an object's properties and mutate (change) them without having to directly reference a property by name could easily lead to some confusion for you or another developer later on, when you're trying to find out why a property of an object has changed.

这在Swift中并不是一件容易的事情(尽管可能),但这并不是坏事:能够迭代对象的属性并变异(更改)它们而不必直接引用属性名称可能很容易导致一些当您试图找出对象的属性发生变化的原因时,您或其他开发人员稍后会感到困惑。

Much better instead to make this kind of operation explicit, and name it correctly. Something like the following:

更好的是将这种操作显式化,并正确命名。类似于以下内容:

extension Object {
    func doubledPrice() -> Object {
        return Object(
            price1: price1 * 2,
            price2: price2 * 2,
            price3: price3 * 2,
            price4: price4 * 2,
            name: name, //we can't double a string
            type: type,
            quantitySold: quantitySold, //I've named the func assuming you won't double the quantitySold, obviously if that's not the desired behaviour then this needs to change
            dateIntroduced: dateIntroduced //can't double a date
        )
    }
}

#2


0  

Make the prices an array? This is from my phone so check for errors. The bad side to this is how messy it can be and how difficult it would be to keep organized.

把价格变成一个数组?这是来自我的手机,所以检查错误。这方面的不好之处在于它是多么混乱以及保持井井有条的难度。

 class MyProduct{
 var price1 : Int
 var price2 : Int
 var price3 : Int

  var pricesArray : [Int]

  init(price1 : Int, price2 : Int, price3 : Int, pricesArray : [Int]){
  self.price1 = price1
  self.price2 = price2
  self.price3 = price3


 for i in 0...2
  { pricesArray.append(0)}
  pircesArray[0] = price1
 pricesArray[1] = price2
  pricesArray[2] = price3


self.pricesArray = pricesArray
 }_

 //then to go over it like
    for i in 0...3{
   banana.pricesArray[i] = banana.procesArray[i] * 2
   }

Or you could make a function in the product class

或者您可以在产品类中创建一个函数

  func equate( sum : Int)
  {
  yourVar = yourVar * sum
  }