unsafe-pointers

How to get bytes out of an UnsafeMutableRawPointer?

这一生的挚爱 提交于 2019-11-27 12:24:37
How does one access bytes (or Int16's, floats, etc.) out of memory pointed to by an UnsafeMutableRawPointer (new in Swift 3) handed to a Swift function by a C API (Core Audio, etc.) load<T> reads raw bytes from memory and constructs a value of type T : let ptr = ... // Unsafe[Mutable]RawPointer let i16 = ptr.load(as: UInt16.self) optionally at a byte offset: let i16 = ptr.load(fromByteOffset: 4, as: UInt16.self) There is also assumingMemoryBound() which converts from a Unsafe[Mutable]RawPointer to a Unsafe[Mutable]Pointer<T> , assuming that the pointed-to memory contains a value of type T: let

Swift 3 UnsafePointer($0) no longer compile in Xcode 8 beta 6

江枫思渺然 提交于 2019-11-27 11:51:47
问题 My code snipet as follows …: let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) { SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)) } … does no longer compile with the following error which I don't understand: "'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type." What to do to fix it? 回答1: From the Release Notes of Xcode 8 beta 6: An Unsafe[Mutable]RawPointer type has been introduced,

Swift 5.0: 'withUnsafeBytes' is deprecated: use `withUnsafeBytes<R>(…)

坚强是说给别人听的谎言 提交于 2019-11-27 04:14:33
I previously used this code in Swift 4.2 to generate an id: public static func generateId() throws -> UInt32 { let data: Data = try random(bytes: 4) let value: UInt32 = data.withUnsafeBytes { $0.pointee } // deprecated warning! return value // + some other stuff } withUnsafeBytes is deprecated on Swift 5.0. How can I solve this? In Swift 5 the withUnsafeBytes() method of Data calls the closure with an (untyped) UnsafeRawBufferPointer , and you can load() the value from the raw memory: let value = data.withUnsafeBytes { $0.load(as: UInt32.self) } (compare How to use Data.withUnsafeBytes in a

How to get bytes out of an UnsafeMutableRawPointer?

你说的曾经没有我的故事 提交于 2019-11-26 15:59:55
问题 How does one access bytes (or Int16's, floats, etc.) out of memory pointed to by an UnsafeMutableRawPointer (new in Swift 3) handed to a Swift function by a C API (Core Audio, etc.) 回答1: load<T> reads raw bytes from memory and constructs a value of type T : let ptr = ... // Unsafe[Mutable]RawPointer let i16 = ptr.load(as: UInt16.self) optionally at a byte offset: let i16 = ptr.load(fromByteOffset: 4, as: UInt16.self) There is also assumingMemoryBound() which converts from a Unsafe[Mutable

Swift 5.0: &#39;withUnsafeBytes&#39; is deprecated: use `withUnsafeBytes<R>(…)

跟風遠走 提交于 2019-11-26 08:14:35
问题 I previously used this code in Swift 4.2 to generate an id: public static func generateId() throws -> UInt32 { let data: Data = try random(bytes: 4) let value: UInt32 = data.withUnsafeBytes { $0.pointee } // deprecated warning! return value // + some other stuff } withUnsafeBytes is deprecated on Swift 5.0. How can I solve this? 回答1: In Swift 5 the withUnsafeBytes() method of Data calls the closure with an (untyped) UnsafeRawBufferPointer , and you can load() the value from the raw memory:

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

时光怂恿深爱的人放手 提交于 2019-11-26 03:20:07
问题 Trying to pass \"self\" to a C function in swift, when calling following code: var callbackStruct : AURenderCallbackStruct = AURenderCallbackStruct.init( inputProc: recordingCallback, inputProcRefCon: UnsafeMutablePointer<Void> ) What is the ideal way to cast \"self\" to a UnsafeMutablePointer type here? 回答1: An object pointer (i.e. an instance of a reference type ) can be converted to a UnsafePointer<Void> (the Swift mapping of const void * , UnsafeRawPointer in Swift 3) and back. In