Questions tagged [unsafemutablepointer]
116 questions
10
votes
1 answer
How to use UnsafeMutablePointer in swift 4
Objective-c has a concept of a pointer to a pointer. If you dereference the first pointer you can access the original
void makeFive(int *n) {
*n = 5;
}
int n = 0;
makeFive(&n);
// n is now 5
When this is bridged to Swift 3 it becomes an…

DerrickHo328
- 4,664
- 7
- 29
- 50
10
votes
2 answers
Swift: Cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'UnsafeMutablePointer'
I have a little Problem with my Code after updating to Swift 3. I had this Code before the conversion:
extension NSData {
func castToCPointer() -> T {
let mem = UnsafeMutablePointer.alloc(sizeof(T.Type))
self.getBytes(mem, length:…

Luke Pistrol
- 1,199
- 3
- 12
- 26
9
votes
2 answers
How to cast UnsafeMutableRawPointer! to UnsafeMutablePointer in Swift 3.0 or newer?
I am working with Accelerate library for my iOS app. I built this app in Swift 2.0 a couple years ago and it was working fine. Now that Swift is updated to 3.0 and 4.0, I have to convert most of the code to the current syntax. I have run the…

Keen R.D.
- 123
- 1
- 5
8
votes
1 answer
Pointers in Swift
I'm trying to understand the use of pointers in Swift, in particular: Unsafe[Mutable]Pointer and UnsafeRaw[Mutable]Pointer. I have several questions on the subject.
Is UnsafePointer equal to const T * Pointer in ? and UnsafeMutablePointer …

emacos
- 541
- 1
- 8
- 17
6
votes
1 answer
Convert UnsafeMutablePointer to UnsafePointer
I'm using a C library in my Swift project, and one of functions requires a UnsafePointer?>! as an in-out argument where I should pass my data. But the problem is that I have this data in…

Eugene Alexeev
- 1,152
- 12
- 32
6
votes
1 answer
How to convert array to UnsafeMutablePointer Swift 3.0?
Here was my workable code in the previous version of Swift:
let imageOptionsDictKeys = [ kCVPixelBufferPixelFormatTypeKey, kCVPixelBufferWidthKey, kCVPixelBufferHeightKey, kCVPixelBufferOpenGLESCompatibilityKey,…

JULIIncognito
- 1,875
- 2
- 15
- 15
6
votes
2 answers
UnsafeMutablePointer in Swift 3
I'm attempting to call SecItemCopyMatching in my keychain utility class in order to get data out of the keychain, yet I'm running into a problem with getting the result argument, UnsafeMutablePointer.
The original statement (in Swift 2,…

Jojodmo
- 23,357
- 13
- 65
- 107
5
votes
2 answers
UnsafeMutablePointer.pointee and didSet properties
I got some unexpected behavior using UnsafeMutablePointer on an observed property in a struct I created (on Xcode 10.1, Swift 4.2). See the following playground code:
struct NormalThing {
var anInt = 0
}
struct IntObservingThing {
var…

kid_x
- 1,415
- 1
- 11
- 31
5
votes
1 answer
Swift UnsafeMutablePointer: Must I call deinitialize before deallocate?
Given an instance of UnsafeMutablePointer, what's the point of calling deinitialize(count:) right before deallocate(capacity:)?
Can't you just call deallocate(capacity:)?
I saw this when reading the section "Using Typed Pointers" of the article…

ma11hew28
- 121,420
- 116
- 450
- 651
5
votes
2 answers
Testcase failed after converting codes from Objective-C to Swift
I am doing some bitwise operations in Swift style, which these codes are originally written in Objective-C/C. I use UnsafeMutablePointer to state the beginning index of memory address and use UnsafeMutableBufferPointer for accessing the element…

Zigii Wong
- 7,766
- 8
- 51
- 79
4
votes
1 answer
UnsafeMutablePointer to an Array element
var a = [1,2,3]
let ptr1 = UnsafeMutablePointer(&a[0]) //works fine
let index = 0
let ptr2 = UnsafeMutablePointer(&a[index]) //compiler throws error
error: cannot invoke initializer for type UnsafeMutablePointer with an argument…

jblixr
- 1,345
- 13
- 34
4
votes
4 answers
Why a struct declared by let can be changed?
In the following code, 'ptr' is a struct declared by let, but its member variable 'pointee' could be changed, Why?
let ptr = UnsafeMutablePointer.allocate(capacity:1)
ptr.pointee = 1

Laughing
- 85
- 6
4
votes
1 answer
Swift deallocate vs free usage
I am allocating bytes using the following call in Swift 3:
let wordSize = 2
let numbytes = 1024*wordsize
var ptr = UnsafeMutableRawPointer.allocate(bytes: numbytes, alignedTo: wordSize)
Question is whether it is correct to deallocate the memory,…

Deepak Sharma
- 5,577
- 7
- 55
- 131
4
votes
3 answers
Mutual non-optional reference cycle in Swift
Consider the following use case:
In a model for some game, you have a Player class. Each Player has an unowned let opponent: Player which represents the opponent they are playing against. These are always created in pairs, and a Player must always…

Jumhyn
- 6,687
- 9
- 48
- 76
4
votes
2 answers
Using C functions in Swift that take functions as arguments
I'm writing a wrapper around a C mathematical library. Every function takes one or two functions as arguments. However, the arguments for those child functions (as well as the parent functions) are not Swifty -hence the wrapper.
I've cleaned up the…

Adam Jones
- 775
- 6
- 23