0

我正在为 UIBezierPath 使用以下扩展:

extension UIBezierPath {

    var elements: [PathElement] {

        var pathElements = [PathElement]()

        withUnsafeMutablePointer(&pathElements) { elementsPointer in

            CGPathApply(CGPath, elementsPointer) { (userInfo, nextElementPointer) in

                let nextElement = PathElement(element: nextElementPointer.memory)

                let elementsPointer = UnsafeMutablePointer<[PathElement]>(userInfo)

                elementsPointer.memory.append(nextElement)
            }
        }
        return pathElements
    }
}

这是来自网站:

https://oleb.net/blog/2015/06/c-callbacks-in-swift/

但是,这与 Swift 3 中断了以下行:

let elementsPointer = UnsafeMutablePointer<[PathElement]>(userInfo)

显示以下错误:

Cannot invade initializer for type 'UnsafeMutablePointer<[PathElement]>' with an argument list of type '(UnsafeMutableRawPointer?)'

我知道这与 Swift 3 的变化有关:UnsafeRawPointerMigration

https://swift.org/migration-guide/se-0107-migrate.html

但是,我不太确定将其移植到 Swift 3 的最佳方法。您能否在这方面提供一些帮助,并为上述扩展提供更新代码?

4

1 回答 1

0

我就这个问题直接联系了 Ole Begemann。他非常乐于助人,并及时提供了适用于 Swift 3 的更新代码。如果其他人也遇到同样的问题,他也会分享相同的代码。

这应该有效:

extension UIBezierPath {

    var elements: [PathElement] {

        var pathElements = [PathElement]()

        withUnsafeMutablePointer(to: &pathElements) { elementsPointer in

            let rawElementsPointer = UnsafeMutableRawPointer(elementsPointer)

            cgPath.apply(info: rawElementsPointer) { userInfo, nextElementPointer in

                let nextElement = PathElement(element: nextElementPointer.pointee)

                let elementsPointer = userInfo?.assumingMemoryBound(to: [PathElement].self)

                elementsPointer?.pointee.append(nextElement)
            }
        }

        return pathElements
    }
}
于 2016-10-03T05:46:33.480 回答