I'm relatively new to Swift and have yet to master the safety aspects of optionals.
I have a dictionary of type [String: [SCNNode]]
. A given molecule will have multiple components as elements in an [SCNNode]
. For that molecule I retrieve this array of components and assign each element to a local SCNNode
to be displayed, manipulated and animated.
let components = moleculeDictionary["aceticAcid"] // the array of components
// [baseMolecule, hydrogenCharge, oxygenCharge, ionizingH, ionizedBond, bonds]
atomsNode_1 = components![0] // baseMolecule
baseNode.addChildNode(atomsNode_1)
atomsNode_5 = components![3] // ionizingH
atomsNode_1.addChildNode(atomsNode_5)
// etc.
In an attempt to optionally bind this, the compiler seems happy with this.
if let node = components?[0] { // baseMolecule
baseNode.addChildNode(node)
}
I'm unclear on the ?. My reading on this suggests we're unwrapping in such a way that we don't care if there's a nil. But does that make this optional binding any better that the forced unwrapping above? Or is this "optional chaining"? Should I be looking instead to just do a check when I assign components
? Should I even be concerened about safety here? The only "upsteam" test I've done is for the presence of the dictionary archive before assigning it to moleculeDictionary
.
I will have hundreds of these assignments so I'd like to get this right. Suggestions on the best way to handle this are welcome!
Thanks, Byrne