Say I have something like this:
class Thing {}
var thing1: Thing? = Thing()
var thing2: Thing? = Thing()
var thing3: Thing? = Thing()
var array: [Thing?] = [thing1, thing2, thing3]
Is there any "idiomatic" way to make thing2 == nil
through the array
?
array[1] = nil
just makes array == [thing1, nil, thing3]
but thing2
remains unmodified, of course.
In the actual code that I have in a project, I have to write a lot of lines like the following:
func clearFlaggedProperties() {
if self.property1 != nil && self.property1!.shouldClear { self.property1 = nil }
if self.property2 != nil && self.property2!.shouldClear { self.property2 = nil }
// and so on...
}
(I only include self
to clarify this particular example, and the check for nil
is related to a bug involving observers.)
How can I better write all that?
I tried tuples. Although I can modify the property through a tuple, I cannot iterate through a tuple with a for in
or forEach
, so I still have to write a lot of if
s.