UIDynamicAnimator items(in:) crashes in iOS 11

烂漫一生 提交于 2019-12-11 04:23:10

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/45774897/uidynamicanimator-itemsin-crashes-in-ios-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!