This comes up a lot (for years) and has me buggered.
- class Act: Codable {
- var a: Uint32
- var b: [UInt32]?
- .
- .
- .
- var z: [UInt32]?
- init( ...vars...)
- }
I want to do a simple sum on .nonzerobitCount for every var a through z.
I can write code to check,e.g.,
10. if Act.b != nil {for index in 0..<Act.b!.count { if Act.b?[index] != nil { totalBits += Act.b?[index].nonzeroBiCount } } }
but even with copy and paste this is exhausting and cludgy.
I don't understand your points:
I tried using optional arrays of optionals. It just made my code messy. It was easier to deal with a formed array that had a zero value than add a couple hundred "!" to optionals calls.
I just proposed to REMOVE optionals
btw: the code "var aToz : [[UInt32]] = []" may work outside a class declaration, as in a playground, but fails within one.
I just tested 1 minute ago, in a class or inside a func, it works perfectly OK of course.
But you need to initialize its content somewhere.
What is the error you get to understand the error you made ?
Anyway, it is good to know it works, don't forget to close the thread.