so I have a swift/Cocoa topic that i just want to understand.
I am asking a Core Animation object, CALayer, for an array of it's sublayers, that looks like this:
let myList = layer.sublayersbut CALayer may or may not have sublayers. so this is an optional List. when I work with the list I have to unwrap it:
myList?.countbut if I wanted to traverse the array, shouldn't just unwrapping it work?
for everyItem in myList?{because it doesn't work. it doesn't remotely work.
XCode throws an error unless I wind up using an exclamation point... which defeats everything I know about conditional unwrapping. So what do I do?
let someItems : [CALayer] = []
if layer.sublayers != nil{
someItem += layer.sublayers
}
for everyItem in someItems{
...
}because that will make me want to kill myself. Conditional unwrapping should HANDLE this. by doing this:
for everyItem in layer.sublayers?{
...
}Swift should be able to manage if sublayers is nil. just don't traverse it. But that's not how it works, I have to Force it every single time. or write at least 4 extra lines of code to work around this... and I'm not just against 4 extra lines of code here... this is an example of a design convention all over the mac os x cocoa era frameworks. I'm really concerned about doing this all the time.
meanwhile, it occurred to me that This behavior is really weird. If CALayer's sublayer property was not optional, this would never happen. they could just return the empty array, and problem solved, good day sir.
I get that Obj-C was different, and we have to expect some speed bumps. I'm just having a difficult time believing that this is the correct way of handling this speed bump.