# Deprecation warning in attempt to convert Apple's reference function in AVCameraCalibrationData.h to object-oriented Swift

**URL:** <https://forums.swift.org/t/deprecation-warning-in-attempt-to-convert-apples-reference-function-in-avcameracalibrationdata-h-to-object-oriented-swift/53602>\
**Category:** Core Libraries\
**Tags:** uikit, av-foundation\
**Created:** [November 19, 2021, 5:58pm UTC](https://forums.swift.org/t/deprecation-warning-in-attempt-to-convert-apples-reference-function-in-avcameracalibrationdata-h-to-object-oriented-swift/53602 "2021-11-19T17:58:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kennystrawnmusic](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kennystrawnmusic/32/20530_2.png) [@kennystrawnmusic](https://forums.swift.org/u/kennystrawnmusic)\
**Post date:** [November 19, 2021, 5:58pm UTC](https://forums.swift.org/t/deprecation-warning-in-attempt-to-convert-apples-reference-function-in-avcameracalibrationdata-h-to-object-oriented-swift/53602/1 "2021-11-19T17:58:10Z")

</div>

After doing days of research, I was able to write the following Swift class that, as you can see, does something similar to the reference example of Line 20 of the [AVCameraCalibrationData.h](https://github.com/xybp888/iOS-SDKs/blob/master/iPhoneOS13.0.sdk/System/Library/Frameworks/AVFoundation.framework/Headers/AVCameraCalibrationData.h) file used to demonstrate how to properly rectify depth data. It compiles fine, but with a deprecation warning denoted by a comment:

```swift
class Undistorter : NSObject {
    
    var result: CGPoint!
    
    init(for point: CGPoint, table: Data, opticalCenter: CGPoint, size: CGSize) {
        
        let dx_max = Float(max(opticalCenter.x, size.width - opticalCenter.x))
        let dy_max = Float(max(opticalCenter.y, size.width - opticalCenter.y))
        let max_rad = sqrt(pow(dx_max,2) - pow(dy_max, 2))
        
        let vx = Float(point.x - opticalCenter.x)
        let vy = Float(point.y - opticalCenter.y)
        
        let r = sqrt(pow(vx, 2) - pow(vy, 2))
        
        // deprecation warning here
        let mag: Float = table.withUnsafeBytes({ (tableValues: UnsafePointer<Float>) in
            
            let count = table.count / MemoryLayout<Float>.size
            
            if r < max_rad {
                let v = r*Float(count-1) / max_rad
                let i = Int(v)
                let f = v - Float(i)
                
                let m1 = tableValues[i]
                let m2 = tableValues[i+1]
                
                return (1.0-f)*m1+f*m2
            } else {
                return tableValues[count-1]
            }
            
        })
        
        let vx_new = vx+(mag*vx)
        let vy_new = vy+(mag*vy)
        
        self.result = CGPoint(
            x: opticalCenter.x + CGFloat(vx_new),
            y: opticalCenter.y + CGFloat(vy_new)
        )
        
    }
}

```

The warning is `'withUnsafeBytes' is deprecated: use withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R instead`. It's a pretty common warning with a lot of examples in existence, but unfortunately I haven't found any examples of rectifying it to fit this use case — all the examples that currently exist of people trying to get it to work involve networking contexts, and they always replace the `UnsafePointer` with an `UnsafeRawBufferPointer` which may work for a `UnsafePointer<UInt8>` but it absolutely doesn't work for a `UnsafePointer<Float>`. If there's a better way to do this, therefore, I'd like to know, because all attempts at resolving this problem over the past day and a half of Googling have come up empty.
