gpt4 book ai didi

ios - 如何使用 Swift 1.2 传递来自 UnsafeMutablePointer 的输入

转载 作者:行者123 更新时间:2023-11-28 07:05:49 25 4
gpt4 key购买 nike

在尝试了多种解决方法后,我陷入了困境。急切寻求帮助。

这是 playground 中的一个基本测试用例:

import JavaScriptCore

func NativeLoggingHook(context: JSContextRef, object: JSObjectRef, thisObject: JSObjectRef, argumentCount: size_t, arguments: [JSValueRef], inout exception: JSValueRef) -> JSValueRef {
// Do something useful
return JSValueMakeUndefined(context)
}

var JSObjectCallAsFunctionCallback: @objc_block (JSContextRef, JSObjectRef, JSObjectRef, size_t, UnsafePointer<JSValueRef>, UnsafeMutablePointer<JSValueRef>) -> JSValueRef = { context, object, thisObject, argumentCount, arguments, exception -> JSValueRef in
let buffer: [JSValueRef] = Array<JSValueRef>(UnsafeBufferPointer(start: arguments, count: argumentCount))
return NativeLoggingHook(context, object, thisObject, argumentCount, buffer, exception) // <- How do I pass the exception to my Swift function?
}

我假设我需要使用 Swift 1.2 将 UnsafePointer 转换为 UnsafeBufferPointer,但如何将 UnsafeMutablePointer 传递给接受输入输出的函数?我尝试了 & 和许多其他变体,但到目前为止没有运气。

更新:

这是我为处理桥接来自 Objective-C 和 Swift 的回调而采用的解决方案。

import JavaScriptCore

func NativeLoggingHook(context: JSContextRef, object: JSObjectRef, thisObject: JSObjectRef, argumentCount: size_t, arguments: [JSValueRef], exception: UnsafePointer<JSValueRef>) -> JSValueRef {
// Do something useful
return JSValueMakeUndefined(context) }

let block: @objc_block (JSContextRef, JSObjectRef, JSObjectRef, size_t, UnsafePointer<JSValueRef>, UnsafeMutablePointer<JSValueRef>)
-> JSValueRef = { context, object, thisObject, argumentCount, arguments, exception -> JSValueRef in
let buffer: [JSValueRef] = Array<JSValueRef>(UnsafeBufferPointer(start: arguments, count: argumentCount))
return NativeLoggingHook(context, object, thisObject, argumentCount, buffer, &exception.memory) }

let imp : COpaquePointer =
imp_implementationWithBlock(unsafeBitCast(block, AnyObject.self))

let callback : JSObjectCallAsFunctionCallback =
unsafeBitCast(imp, JSObjectCallAsFunctionCallback.self)

func setNativeHook(hook: JSObjectCallAsFunctionCallback) -> Bool {
// Do something useful
return true }

println(setNativeHook(callback))

最佳答案

我不知道这是否真的适合你,但我通过解开指针包装器并通过引用传递结果来编译它:

import JavaScriptCore

func NativeLoggingHook(context: JSContextRef, object: JSObjectRef, thisObject: JSObjectRef, argumentCount: size_t, arguments: [JSValueRef], inout exception: JSValueRef) -> JSValueRef {
// Do something useful
return JSValueMakeUndefined(context)
}

var JSObjectCallAsFunctionCallback: @objc_block (JSContextRef, JSObjectRef, JSObjectRef, size_t, UnsafePointer<JSValueRef>, UnsafeMutablePointer<JSValueRef>) -> JSValueRef = {
(context, object, thisObject, argumentCount, arguments, exception) -> JSValueRef in

let buffer: [JSValueRef] = Array<JSValueRef>(UnsafeBufferPointer(start: arguments, count: argumentCount))
return NativeLoggingHook(context, object, thisObject, argumentCount, buffer, &exception.memory)
// <- pass the underlying memory type by reference?
}

关于ios - 如何使用 Swift 1.2 传递来自 UnsafeMutablePointer 的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30652798/

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