unsafe-pointers

Getting pointer for first entry in an array

江枫思渺然 提交于 2019-12-22 02:03:02
问题 I want to get pointer of first entry in the array. This is how I tried int[] Results = { 1, 2, 3, 4, 5 }; unsafe { int* FirstResult = Results[0]; } Get following compilation error. Any ideas how to fix it? You can only take the address of an unfixed expression inside of a fixed statement initializer 回答1: The error codes are magic to get the answer - search for error code (CS0212 in your case) and you get explanation with proposed fix in a lot of case. Search: http://www.bing.com/search?q

Getting pointer for first entry in an array

Deadly 提交于 2019-12-22 02:02:17
问题 I want to get pointer of first entry in the array. This is how I tried int[] Results = { 1, 2, 3, 4, 5 }; unsafe { int* FirstResult = Results[0]; } Get following compilation error. Any ideas how to fix it? You can only take the address of an unfixed expression inside of a fixed statement initializer 回答1: The error codes are magic to get the answer - search for error code (CS0212 in your case) and you get explanation with proposed fix in a lot of case. Search: http://www.bing.com/search?q

UnsafePointer<UInt8> initializer in Swift 3

 ̄綄美尐妖づ 提交于 2019-12-20 17:50:15
问题 I have a receipt validation class that is deprecated since Swift 3 has released. I fixed some issues, but I still have many ... Here is the GitHub source code I used : https://gist.github.com/baileysh9/4386ea92b047d97c7285#file-parsing_productids-swift and https://gist.github.com/baileysh9/eddcba49d544635b3cf5 First Error : var p = UnsafePointer<UInt8>(data.bytes) Compiler throws : Cannot invoke initializer for type UnsafePointer(UInt8) with an argument list of type UnsafeRawPointer Second

Cast to different C struct unsafe pointer in Swift

南笙酒味 提交于 2019-12-19 10:05:14
问题 I want to call the Posix socket functions socket and bind from Swift. socket is pretty easy—it takes Int32 s, but bind is causing a problem, because I have a sockaddr_in pointer, but it wants a sockaddr pointer. In C, this would be a cast, like: bind(sock, (struct sockaddr *)&sockAddress, sizeof(sockAddress)) Here's an attempt in Swift: let sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) var sockAddress = sockaddr_in() bind(sock, &sockAddress, UInt32(MemoryLayout<sockaddr_in>.size)) The bind

Swift UnsafeMutablePointer<Unmanaged<CFString>?> allocation and print

ぃ、小莉子 提交于 2019-12-17 18:59:31
问题 I'm new to swift and I have some difficulties to deal with pointers of unmanaged CFString (or NSString). I'm working on a CoreMIDI project that implies usage of UnsafeMutablePointer?> as you can see in this function : func MIDIObjectGetStringProperty(_ obj: MIDIObjectRef, _ propertyID: CFString!, _ str: UnsafeMutablePointer<Unmanaged<CFString>?>) -> OSStatus My problem is that I want to allocate a buffer to receive the content of the property (_str) then call the function above, and finally

What is the correct way to reinterpret an entity in Swift?

谁都会走 提交于 2019-12-12 22:22:54
问题 There are cases when you have to deal with a structure of certain type, but the upstream API requires you to present it though a pointer to another type at other places. For instance, Unix Bind is expecting its second argument to be a pointer to a sockaddr , whereas the constructor should be sockaddr_in . For now I'm sticking to a two-layer with* : var sa = sockaddr_in(/* ... */) withUnsafePointer(to: &sa) { _sa in _sa.withMemoryRebound(to: sockaddr.self, capacity: 1) { __sa in let err = bind

'init is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type

梦想与她 提交于 2019-12-12 13:46:33
问题 since i converted my Code to Swift 3 the error occurs. 'init is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type. Here is my Code: func parseHRMData(data : NSData!) { var flags : UInt8 var count : Int = 1 var zw = [UInt8](count: 2, repeatedValue: 0) flags = bytes[0] /*----------------FLAGS----------------*/ //Heart Rate Value Format Bit if([flags & 0x01] == [0 & 0x01]) { //Data Format is set to UINT8 //convert UINT8 to UINT16 zw

string.withCString and UnsafeMutablePointer(mutating: cstring) wrapped into a function

旧时模样 提交于 2019-12-11 05:56:15
问题 I have a problem with the String.withCString{} in combination with UnsafeMutablePointer(mutating: ...). Given: a C-Function like this randomSign(xml_string: UnsafeMutablePointer<Int8>!) -> Uint Also given: a String like str = "some xml_tag" My working code is like this (VERSION_1) func randomSignWrapper_1(xml_tag_str: String) -> Uint { let result = xml_str.withCString { s in return randomSign(UnsafeMutablePointer(mutating: s)) } return result } But I want the withCString to be put into a

How to convert array to UnsafeMutablePointer<UnsafeRawPointer?> Swift 3.0?

女生的网名这么多〃 提交于 2019-12-08 21:58:20
问题 Here was my workable code in the previous version of Swift: let imageOptionsDictKeys = [ kCVPixelBufferPixelFormatTypeKey, kCVPixelBufferWidthKey, kCVPixelBufferHeightKey, kCVPixelBufferOpenGLESCompatibilityKey, kCVPixelBufferIOSurfacePropertiesKey] let imageOptionsDictValues = [ cvPixelFormatType, frameW, frameH, boolYES] var keyCallbacks = kCFTypeDictionaryKeyCallBacks var valueCallbacks = kCFTypeDictionaryValueCallBacks let imageOptions = CFDictionaryCreate(kCFAllocatorDefault,

Preparing for Swift 4 - UnsafeMutablePointer migration to UnsafeMutableBufferPointer

…衆ロ難τιáo~ 提交于 2019-12-08 11:03:02
问题 I've got a char-by-char function that returns a character from a file. It is pretty performant, as far as i now. Xcode says that the characterPointer.initialize(from: s.characters) "will be removed in Swift 4.0." And I have to "use 'UnsafeMutableBufferPointer.initialize(from:)' instead". But I can't get it. Can you please explain it to me. How to use a quick iterator over characters with a UnsafeMutableBufferPointer? Do you have an example? This is my function: /// Return next character, or