我有一个内部类,它采用UIScrollViewDelegate
,但不实现scrollViewWillEndDragging(_:withVelocity:targetContentOffset:)
.我想使用class_addMethod
将此方法添加到类中,但不幸的是,在try 将新值赋给targetContentOffset
时遇到了EXC_BAD_ACCESS
错误.
以下是我的代码:
func extendDelegate(scrollViewDelegte: UIScrollViewDelegate) {
let block : @convention(block) (UIScrollView, CGPoint, UnsafeMutablePointer<CGPoint>) -> Void = { scrollView, velocity, targetContentOffset in
// EXC_BAD_ACCESS here.
// I can read the pointee and it does contain a valid CGPoint value,
// however assigning to it gives a crash.
targetContentOffset.pointee = .zero
}
class_addMethod(
type(of: scrollViewDelegte),
#selector(UIScrollViewDelegate.scrollViewWillEndDragging(_:withVelocity:targetContentOffset:)),
imp_implementationWithBlock(block),
"v@:@{CGPoint=dd}^{CGPoint=dd}"
)
}
如有任何帮助,不胜感激!