问题描述
在iOS 11(Xcode 9 beta 5)中,我正在调用UIDynamicAnimator的items(in:)
方法并立即崩溃:
In iOS 11 (Xcode 9 beta 5), I'm calling UIDynamicAnimator's items(in:)
method and immediately crashing:
这是怎么回事?
推荐答案
您发现了一个错误. (向Apple提交,雷达33979954.)希望它会尽快修复,但在此之前,这里是修复方法:
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}
}
}
现在调用view(in:)
而不是items(in:)
,一切都会好起来.
Now call view(in:)
instead of items(in:)
, and all will be well.
问题是将虚假对象放入从items(in:)
返回的数组中.由于存在这些虚假对象,因此数组无法跨越从Objective-C到Swift的桥梁;在Swift中将返回的数组键入为[UIDynamicItem]
,但是该数组包含的内容不是UIDynamicItem对象.
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.
此扩展程序通过不越桥来解决此问题.我们留在NSArray Objective-C世界中,过滤掉虚假对象,然后然后过桥.
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.
这篇关于UIDynamicAnimator项目(in :)在iOS 11中崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!