gpt4 book ai didi

Kotlin-Multiplatform 中的 CPointer

转载 作者:行者123 更新时间:2023-12-02 13:12:07 25 4
gpt4 key购买 nike

我找不到任何关于如何在 Kotlin Multiplatform 中获取 CPointer 的示例,现有的文档也没有多大帮助。在我的 iOS 源集中,我需要构建与以下 Swift 代码等效的 Kotlin(仅包括代码的相关部分):

  ...(hex: String) {

if hex.hasPrefix("#") {
let start = hex.index(hex.startIndex, offsetBy: 1)
let scanner = Scanner(string: hexColor)
var hexNumber: UInt64 = 0



if scanner.scanHexInt64(&hexNumber) {

r = CGFloat((hexNumber & 0xff000000) >> 24) / 255
....

我遇到问题的具体部分是

scanner.scanHexInt64(&hexNumber)

这是 Kotlin 代码和问题

//input to function - hex: String
val scanner = NSScanner(hex)
if (hex.startsWith("#")) {
scanner.scanLocation = 1u
}
var hexNumber : UInt32 = 0u
/*Type mismatch.
Required:
CPointer<UIntVar /* = UIntVarOf<UInt> */>?
Found:
UInt32 /* = UInt */
*/
//HOW TO GET CPOINTER TO hexNumber?
scanner.scanHexInt(hexNumber)

根据文档:( link)

Pointers and arrays are mapped to CPointer<T>?.

但是如何呢?

最佳答案

找到了我的问题的答案。不得不用

memScoped

.

memScoped {
var pointed : UIntVar = alloc<UIntVar>()
scanner.scanHexInt(pointed.ptr)
val alpha: CGFloat = 1.0
val pointedValue = pointed.value
val r: CGFloat = (((pointedValue and 0xFF0000) shr 16)/255.0)
....
}

互联网上关于此的唯一来源(关于应用它来获取指针)在这里 - link

memScoped

inline fun <R> memScoped(block: MemScope.() -> R): R
Runs given block providing allocation of memory which will be automatically disposed at the end of this scope

在其中,使用扩展函数alloc()获取CVariable

fun <reified T : CVariable> NativePlacement.alloc(): T

然后您可以通过另一个扩展函数访问指针

val <T : CPointed> T.ptr: CPointer<T>

回想起来一切都非常清楚,并且清楚地知道最初以错误的方式解决了问题,希望通过类似 CPointer<UInt> = ... 的方式获得指针官方文档位于 https://kotlinlang.org/docs/reference/native/c_interop.html

关于Kotlin-Multiplatform 中的 CPointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59690974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com