In Swift 2, NSMutableArray stored things of type AnyObject, but in Swift 3 the type is changed to Any. This means that optionals, which are enums, can be stored.
Optionals were bridged to an opaque object that was not accessible in Objective-C, and that is probably still the case, but a proposal has been accepted (SE-0140) that changes this:
Anyshould raise a warning unless the conversion is made explicit. When an
Optional<T>value does end up in an
Any, and gets bridged to an Objective-C object, if it contains
somevalue, that value should be bridged; otherwise,
NSNullor another sentinel object should be used.
This was accepted a few weeks ago, and I don't know when that will be implemented and shipped in Xcode.
(I haven't checked, but I suspect other NSMutableXXX containers can suffer the same issue)
Indeed. I generally avoid this issue by working in ‘Swift space’ as much as possible. That way I get strict type checking right up until the last minute. To continue your example, I’d write this:
var anArray: [Foo] =  let something = callSomethingThatReturnsAnOptionalObject() anArray.append(something) coder.encode(anArray as NSArray)
which fails to compile because
append(_:)won’t accept a
This doesn’t work all the time (for example, if the array is heterogeneous) but it’s helpful in many cases.
Share and Enjoy
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"