In iOS 11 (Xcode 9 beta 5), I'm calling UIDynamicAnimator's items(in:)
method and immediately crashing:
NSArray element failed to match the Swift Array Element type
What's going on?
In iOS 11 (Xcode 9 beta 5), I'm calling UIDynamicAnimator's items(in:)
method and immediately crashing:
NSArray element failed to match the Swift Array Element type
What's going on?
You've found a bug. (Filed with Apple, radar 33979954.) Hopefully it will be fixed soon, but until then, here's the fix:
extension UIDynamicAnimator {
func views(in rect: CGRect) -> [UIView] {
let nsitems = self.items(in: rect) as NSArray
return nsitems.flatMap{$0 as? UIView}
}
}
Now call view(in:)
instead of items(in:)
, and all will be well.
The problem is that spurious objects are being put into the array returned from items(in:)
. Because of these spurious objects, the array cannot cross the bridge from Objective-C to Swift; the returned array is typed in Swift as [UIDynamicItem]
, but the array contains things that are not UIDynamicItem objects.
The extension works around this by not crossing the bridge. We stay in the NSArray Objective-C world, filter out the spurious objects, and then cross the bridge.