mercredi 24 mars 2021

Swift - How could I iterate over all possibilities of a collection of stridable variables

I have a configuration defined with various ranges and would like to iterate over all the possibilities without coding it explicitly.

My guess is to use reflection but perhaps there is an easier way?

here is an example:

struct Settings {
    var a = stride(from: 0, to: 24, by: 1)
    var b =  stride(from: 0, to: 4, by: 1)
    var c = stride(from: 0, to: 10, by: 2)
    var d = stride(from: 0, to: 10, by: 2)
    var e = stride(from: 0.0, to: 0.5, by: 0.05)
    var f = stride(from: 0, to: 10, by: 1)
}

I want to avoid nested loops if possible. i..e this isn't great:

let settings = Settings()
for v1 in settings.a {
 for v2 in settings.b {
  for v3 in settings.c {
   ...
    ...
    }
   }
  }
 }
}


would love to have something similar to CaseIterable which could provide all of the combinations





Aucun commentaire:

Enregistrer un commentaire