unsafe-pointers

Swift 4.1 deinitialize and deallocate(capacity:) deprecated

亡梦爱人 提交于 2021-02-07 14:24:03
问题 I've been saying this to form a C array of CGPoint: let arr = UnsafeMutablePointer<CGPoint>.allocate(capacity:4) defer { arr.deinitialize() arr.deallocate(capacity:4) } arr[0] = CGPoint(x:0,y:0) arr[1] = CGPoint(x:50,y:50) arr[2] = CGPoint(x:50,y:50) arr[3] = CGPoint(x:0,y:100) Now (Swift 4.1 in the Xcode 9.3 beta) both deinitialize and deallocate(capacity:) are deprecated. It looks like what I'm supposed to say now might be: defer { arr.deinitialize(count:4) arr.deallocate() } Is that right?

Warning: Initialization of 'UnsafeBufferPointer<T>' results in a dangling buffer pointer

天涯浪子 提交于 2020-05-12 11:55:49
问题 After update to Swift 5.2 / Xcode 11.4 got a warning to following code: extension Data { init<T>(from value: T) { var value = value let pointer = UnsafeBufferPointer(start: &value, count: 1) self.init(buffer: pointer) } func to<T>(type: T.Type) -> T { return self.withUnsafeBytes { $0.load(as: T.self) } } } On line let pointer = UnsafeBufferPointer(start: &value, count: 1) I got Initialization of 'UnsafeBufferPointer' results in a dangling buffer pointer I can use @silenceWarning but it's

Warning: Initialization of 'UnsafeBufferPointer<T>' results in a dangling buffer pointer

人盡茶涼 提交于 2020-05-12 11:55:47
问题 After update to Swift 5.2 / Xcode 11.4 got a warning to following code: extension Data { init<T>(from value: T) { var value = value let pointer = UnsafeBufferPointer(start: &value, count: 1) self.init(buffer: pointer) } func to<T>(type: T.Type) -> T { return self.withUnsafeBytes { $0.load(as: T.self) } } } On line let pointer = UnsafeBufferPointer(start: &value, count: 1) I got Initialization of 'UnsafeBufferPointer' results in a dangling buffer pointer I can use @silenceWarning but it's

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

≡放荡痞女 提交于 2020-03-04 23:16:54
问题 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 Objective-C

Swift conversion: ERROR - UnsafeMutablePointer

那年仲夏 提交于 2020-02-25 04:45:26
问题 I am attempting to convert my Swift 2 code into the latest syntax(Swift 3). I am receiving the following error: Cannot invoke initializer for type 'UnsafeMutablePointer<CUnsignedChar>' with an argument list of type '(UnsafeMutableRawPointer!) Swift 2 Code: let rawData = UnsafeMutablePointer<CUnsignedChar>(calloc(height * width * 4, Int(sizeof(CUnsignedChar)))) Can someone please help me resolve this conversion syntax issue? 回答1: calloc returns a "raw pointer" (the Swift equivalent of void *

Swift conversion: ERROR - UnsafeMutablePointer

北城以北 提交于 2020-02-25 04:45:09
问题 I am attempting to convert my Swift 2 code into the latest syntax(Swift 3). I am receiving the following error: Cannot invoke initializer for type 'UnsafeMutablePointer<CUnsignedChar>' with an argument list of type '(UnsafeMutableRawPointer!) Swift 2 Code: let rawData = UnsafeMutablePointer<CUnsignedChar>(calloc(height * width * 4, Int(sizeof(CUnsignedChar)))) Can someone please help me resolve this conversion syntax issue? 回答1: calloc returns a "raw pointer" (the Swift equivalent of void *

Swift convert Data to UnsafeMutablePointer<Int8>

守給你的承諾、 提交于 2020-01-14 03:16:04
问题 I'm calling a function in an objective c class from swift. -(char *)decrypt:(char *)crypt el:(int)el{} when calling this function from swift, it asks for an UnsafeMutablePointer<Int8> as the value for the parameter 'crypt' the value for the 'crypt' is comming from a server and it is a base64encoded string. So I decode that string and got a Data object. let resultData = Data(base64Encoded: base64String) Now I need to pass this data to the above mentioned function. I have tried to convert this

Cast a Swift struct to UnsafeMutablePointer<Void>

倖福魔咒の 提交于 2019-12-29 05:16:10
问题 Is there a way to cast a Swift struct's address to a void UnsafeMutablePointer? I tried this without success: struct TheStruct { var a:Int = 0 } var myStruct = TheStruct() var address = UnsafeMutablePointer<Void>(&myStruct) Thanks! EDIT: the context I am actually trying to port to Swift the first example in Learning CoreAudio . This is what I have done until now: func myAQInputCallback(inUserData:UnsafeMutablePointer<Void>, inQueue:AudioQueueRef, inBuffer:AudioQueueBufferRef, inStartTime

dereference and advance pointer in one statement?

南楼画角 提交于 2019-12-24 05:58:10
问题 I'm reading from a byte array as follows: int* i = (int*)p; id = *i; i++; correct me if I'm wrong, but ++ has precedence over *, so is possible to combine the *i and i++ in the same statement? (e.g. *i++) (this is technically unsafe C#, not C++, p is a byte*) 回答1: I believe that id = *i; i++; and id = *i++; are equivalent. The ++ operator, when used as a suffix (e.g. i++ ), returns the value of the variable prior to the increment. I'm somewhat confused by the reflector output for unsafe class

Go: convert unsafe.Pointer to function pointer and vice versa

↘锁芯ラ 提交于 2019-12-22 09:47:35
问题 In C you can put function pointers into an array of void pointers and convert them back to function pointers of any type: extern int (*fn1)(void); extern void (*fn2)(int); void foo(void) { void *array[2]; int i; /* implicit cast from function pointer to void pointer */ array[0] = fn1; array[1] = fn2; for (i = 0; i < 2; i++) { int (*fp)(int, int, int); /* implicit cast from void pointer to function pointer */ fp = array[i]; /* call function with a different signature */ fp(1, 2, 3); } } I need