Error in UnsafeMutablePointer in swift3

Sachin

My codd look like this

let samples = UnsafeMutableBufferPointer<Int16>(start:UnsafeMutablePointer(buffer.mData), count: Int(buffer.mDataByteSize)/sizeof(Int16))

While running this code is generating the following error

Cannot invoke initializer for type 'UnsafeMutablePointer<_>' with an argument list of type '(UnsafeMutableRawPointer?)'

buffer.mdata is having raw data. How can I solve this issue. Thanks in advance

Martin R

Assuming that buffer is a AudioBuffer from the AVFoundation framework: buffer.mData is a "optional raw pointer" UnsafeMutableRawPointer?, and in Swift 3 you have to bind the raw pointer to a typed pointer:

let buffer: AudioBuffer = ...

if let mData = buffer.mData {
    let numSamples = Int(buffer.mDataByteSize)/MemoryLayout<Int16>.size
    let samples = UnsafeMutableBufferPointer(start: mData.bindMemory(to: Int16.self, capacity: numSamples),
                                             count: numSamples)
    // ...
}

See SE-0107 UnsafeRawPointer API for more information about raw pointers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to cast self to UnsafeMutablePointer<Void> type in swift

"Empty collection literal requires an explicit type" error on Swift3

How to use UnsafeMutablePointer in Swift 3?

Cast a Swift struct to UnsafeMutablePointer<Void>

Swift 2 - UnsafeMutablePointer<Void> to object

What is UnsafeMutablePointer<Unmanaged<CMSampleBuffer>?> in Swift?

Swift error: '&' used with non-inout argument of type 'UnsafeMutablePointer'

How to create UnsafeMutablePointer<CGPoint> Object in swift

UnsafeMutablePointer<CFTypeRef> in Swift 3

Convert UnsafeMutableRawPointer to UnsafeMutablePointer<T> in swift 3

UnsafeMutablePointer to expected argument type UnsafeMutablePointer<_> in Swift 3

Swift: Cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'UnsafeMutablePointer'

Casting a variable of type 'UnsafeMutableRawPointer' to UnsafeMutablePointer<> in Swift 3

How to use UnsafeMutablePointer<T?> in swift 3

In Swift 3.1, UnsafeMutablePointer.initialize(from:) is deprecated

How to use UnsafeMutablePointer<OpaquePointer> in Swift?

Swift convert Data to UnsafeMutablePointer<Int8>

Swift UnsafeMutablePointer & UnsafeMutablePointer<UnsafePointer<SomeType>>

Using UnsafeMutablePointer and CFRunLoopObserverContext in swift 2

Cast a swift dictionary ( [String:AnyObject] ) to a UnsafeMutablePointer

UnsafeMutablePointer<Bytef> swift3

Ios Swift3 : lazy var fetchrequestcontroller showing an error

Compilation error in swift3: 'AnyObject' is not a subtype of 'NSObject'

Fatal error when unwrapping a segued string (swift3)

Swift3 JSON Error - Cannot subscript value of type AnyObject

How to create UnsafeMutablePointer<UnsafeMutablePointer> in swift

SceneKit Swift3 compiler-error

Create check for button that will create a error (swift3)

Error in encoding date format to use it in json in swift3