- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Swift 很陌生。尝试编写一个调用外部 C 方法的 Swift 应用程序。但是,Xcode 编译器给出了有关类型转换的错误。 C 方法原型(prototype)的形式为:
void * cMethod(const char* string1Path, const char* string2Path, const char* string3Path, const char* string4Path);
Swift 代码的要点是:
import OpenGLES
import CoreGraphics
import GLKit
var mGLUTWindow: Void?
class myClass {
mGLUTWindow = nil
let appBundle = Bundle.main
let string1 = appBundle.path(forResource: "Init", ofType: "dat")
let string1Path = Int8((string1?.description)!)
let string2 = appBundle.path(forResource: "autostart", ofType: "type")
let string2Path = Int8((string2?.description)!)
let string3: String? = appBundle.path(forResource: "data", ofType: "") ?? "" + ("/")
let string3Path = Int8((string3?.description)!)
mGLUTWindow = cMethod(UnsafePointer(string3Path), string1Path, string2Path, "ios")
}
编译器给出错误:
Cannot convert value of type 'Int8?' to expected argument type 'UnsafePointer<Int8>?'
C 方法由桥接头引入。有没有办法将其转换为预期的参数?
注意:这里的 Swift 代码是从几年前的 Objective-C 文件中引入的,并且正在适应 Swift。我用过this Obj-C 到 Swift 转换器,因为我几乎不懂 Swift,也无法阅读 Obj-C。我放入的 Obj-C 内衬的形式为:
NSString * string1 = [[appBundle pathForResource:@"data" ofType:@""] stringByAppendingString:@"/"];
const char * string1Path = [string1 cStringUsingEncoding:[NSString defaultCStringEncoding]];
最佳答案
如果给我 C 函数和一些显示为 Objective-C 行的代码,我会写如下内容:
var mGLUTWindow: UnsafeRawPointer?
class MyClass {
func aMethod() {
let appBundle = Bundle.main
let string1 = appBundle.path(forResource: "Init", ofType: "dat")!
let string2 = appBundle.path(forResource: "autostart", ofType: "type")!
let string3 = (appBundle.path(forResource: "data", ofType: "") ?? "") + ("/")
mGLUTWindow = UnsafeRawPointer(cMethod(string3, string1, string2, "ios"))
}
}
在 Swift 中,当您将 String
传递给 char *
类型的参数时,Swift 会生成一个临时 C 字符串缓冲区并传递该缓冲区的地址,您有无需调用类似 const char * string1Path = [string1 cStringUsingEncoding:[NSString defaultCStringEncoding]];
的内容。
如果cMethod
保留任何指针供以后使用,您可能需要更复杂的代码,但不清楚我们是否需要像当前描述那样复杂的代码。
关于转换 Int8?到 UnsafePointer<Int8>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55622764/
给定 public struct MIDIPacketList { public var numPackets: UInt32 public var packet: (MIDIPack
我第一次尝试快速使用 CommonCrypto(在 https://github.com/sergejp/CommonCrypto 的帮助下)。这是我的代码: UnsafeRawPointer(ivD
我正在为 OS X 使用 Swift 中的外部 C 库。我得到一个值 cda,它在 C 中定义为 double* (它是一个指向 double 组的指针)。 当导入到 Swift 中时,它会将类型识别
我正在尝试更新我在 http://mattg411.com/swift-coremidi-callbacks/ 找到的 CoreMidi 示例中的代码 代码是 Swift 3 之前的代码,所以我需要做
我正在编写一个需要与 C 库进行互操作的 Swift 程序。此 C 库包含一组使用回调的函数。我解决了如何将 Swift 函数传递给这些 C 函数的问题,但我很难将 Swift 原生类型转换为适当的
当我定义行时出现构建错误 let runFont : CTFontRef = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttribu
我正在尝试访问 AVAudioPCMBuffer.floatChannelData使用 Swift 但它的类型是 UnsafePointer> (在 Objective-C 中,@property(n
在 Swift 中,我想传递 data 类型的数据缓冲区(名为 Data )到一个 C 函数(名为 do_something ),它采用 UnsafePointer 类型的指针. 下面的代码示例是否正
我正在与运行 C++ 代码并向 iOS 设备发送几乎每秒 10 次实时数据的硬件设备进行交互。 我有一个像这样的 Swift 方法: func m16b_to_mE0(_ pointer: Unsaf
以下单元测试总是失败,因为一些 UnsafePointer 等于其他一些。我观察到至少 3 个字符串的这种行为。我在 High Sierra 版本 10.13.1 (17B48) 上使用 Xcode
我正在尝试转换 UnsafePointer到UnsafePointer到目前为止,我以这个解决方案结束: let bufferSize = 1024 let buffer: UnsafePointer
我希望能够从不可变值创建一个 UnsafePointer。 我尝试做的最简单的复制如下: let number : Int = 42; var pointer = UnsafePointer(&num
我发现以下代码可以编译并运行: func foo(p:UnsafePointer) { var p = p for p; p.memory != 0; p++ { pr
尝试清除我的 SWIFT 5/XCODE 项目中的一些警告,但我陷入了困境: let sendBytes:[UInt8] = [0x0, 0x0, 0x5, 0x0] let msgData =
所以以前我打开了Initialization of 'UnsafePointer' results in a dangling pointer,但是mods说它是重复的。 但是我不认为这是重复的,因为
我对 Swift 很陌生。尝试编写一个调用外部 C 方法的 Swift 应用程序。但是,Xcode 编译器给出了有关类型转换的错误。 C 方法原型(prototype)的形式为: void * cMe
据我了解,UnsafePointer 将指针对象呈现为不可变的,而 UnsafeMutablePointer 将指针对象呈现为可变的。但是 vDSP 函数的签名 vDSP_zrvmul如下: func
我试过这个方法: String.stringWithCString(cString: UnsafePointer, encoding: NSStringEncoding) 但不幸的是,它只适用于 Un
我有一个二进制文件,其中包含许多 Float 值的三元组;它实际上是一长串 SCNVector3,表示 SCNGeometrySource 的顶点。我将文件(请原谅遗漏了 do-try-catch 等
如何调用以下函数: func AXUIElementCopyAttributeNames(element: AXUIElement!, names: UnsafePointer?>) -> AXErr
我是一名优秀的程序员,十分优秀!