Questions tagged [unsafe-pointers]

Some "safe" languages like Swift and Go offer "unsafe" pointers for use with APIs written in languages like C and Objective-C. Questions using this tag should also have the relevant language tag, e.g. [swift] or [go].

250 questions
0
votes
0 answers

Preparing for Swift 4 - UnsafeMutablePointer migration to UnsafeMutableBufferPointer

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…
Peter Shaw
  • 1,867
  • 1
  • 19
  • 32
0
votes
1 answer

From NSTextfield to (const unsigned char *) with Swift

How can i properly received a (const unsigned char *) in a C program from a NSTextField in Swift ? Case 1 : let string1 = atextfield!.stringValue // -> "Test" print(string1) // -> "Test" string1/_core/_baseAddress = nil // in debug let string2 =…
Stephane
  • 391
  • 1
  • 2
  • 13
0
votes
0 answers

How to pass image data to Swift MagicWand?

The read() API accept data type but how does this read data? https://github.com/naithar/MagickWand/blob/master/Sources/MagickWand/Wand/ImageWand/ImageWand%2BRead.swift#L51 let urlImage:URL? = URL(fileURLWithPath: "files/test.png") do { …
James Lei
  • 340
  • 2
  • 5
  • 15
0
votes
1 answer

Swift dictionary doesn't want to populate from C Callback function

I'm writing a PDF Parser in Swift and I've reached the point where I get all the font data with a callback function (CGPDFDictionaryApplyFunction), the getFont function is supposed to populate the fonts dictionary in the PDFFontCollection…
0
votes
0 answers

Can not initialize UnsafePointer

I am trying to initialize an unsafe pointer func f1() { var ifaddr : UnsafeMutablePointer? // .... // .... collect ifaddr value var d1 = UnsafePointer(ifaddr) } and received the below error in the last line…
0
votes
1 answer

Convert CUnsignedChar pointer to CChar pointer

I have an objective C function that gives me a UnsafePointer/UnsafePointer. However I need this data as a UnsafePointer unfortunately the function I require doesn't take UnsafePointer.
0
votes
1 answer

How do I update a statement of Data of UnsafePointer for Swift 3?

I'm getting the error below for this statement: let data = Data(bytes: UnsafePointer(cubeData), count: cubeData.count * MemoryLayout.size) cubeData is defined as: var cubeData = [Float](repeating: 0, count: size * size * size *…
ICL1901
  • 7,632
  • 14
  • 90
  • 138
0
votes
2 answers

Error in UnsafeMutablePointer in swift3

My codd look like this let samples = UnsafeMutableBufferPointer(start:UnsafeMutablePointer(buffer.mData), count: Int(buffer.mDataByteSize)/sizeof(Int16)) While running this code is generating the following error Cannot invoke initializer for…
Sachin
  • 135
  • 9
0
votes
1 answer

NSMutableData's bytes to UnsafeMutableRawPointer not updating the value of mutableData

I am having a struct object. And a method, whose input is payload. Now I am creating a mutableData named packet, and it's mutable bytes are referring to ICMPHeader struct. struct ICMPHeader { var type:UInt8 var code:UInt8 var…
Ankit Thakur
  • 4,739
  • 1
  • 19
  • 35
0
votes
0 answers

Data, UnsafeMutablePointer and extended attributes in Swift 3

In swift 2 I had an NSURL extension for working with extended attributes and it included this: func setAttribute(name: String, value: String) { let data = value.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)! …
dbmrq
  • 1,451
  • 13
  • 32
0
votes
1 answer

Using C in Swift - strlcpy - Cannot convert value of type 'UnsafeMutablePointer<_>'

I am trying to use strlcpy in Swift 3.0, but keep getting the error "Cannot convert value of type 'UnsafeMutablePointer<_>' to expected argument type 'UnsafeMutablePointer!'" Here is my code func login(portal: String, username: String, password:…
Lneuner
  • 1,090
  • 1
  • 9
  • 19
0
votes
1 answer

Casting UnsafeMutablePointers to UnsafeMutableRawPointers

I have a couple of issues updating to swift 3.0. I have the following code: // Retrieve the Device GUID let device = UIDevice.current let uuid = device.identifierForVendor let mutableData = NSMutableData(length: 16) …
Tom
  • 790
  • 2
  • 9
  • 32
0
votes
1 answer

Idiomatic way in Swift to typecast an UnsafeMutablePointer to an Array of non-void types?

Many C/Obj-C APIs deal in pointers to void. In swift, these types are typically represented as UnsafeMutablePointer or UnsafePointer (for const void*). What is the typical way in Swift to reinterpret these values to a packed array of data, such as…
Fooberman
  • 626
  • 5
  • 14
0
votes
1 answer

copy NSData to UnsafeMutablePointer

Hi there stackoverflowers. I'm implementing a wrapper for Secure Transport and I'm stuck on some of the C -> Swift syntax. func sslReadCallback(connection: SSLConnectionRef, data: UnsafeMutablePointer, var dataLength:…
Mike S
  • 4,092
  • 5
  • 35
  • 68
0
votes
1 answer

How to write an unsafepointer of type float in MPSImageDilate

I am new to Unsafepointer. In the following in this documentation, it says I need the values to be of type Unsafepointer. I need it to be equivalent of Array of 139*139 elements of repeatedValues of 1.0 (floating number).…
Nasiba
  • 321
  • 2
  • 15