Tag: swift

从Swift调用C ++函数

我应该如何从Swift文件中调用C ++函数(不涉及类)? 我试过这个: 在someCFunction.c中 : void someCFunction() { printf(“Inside the C function\n”); } void aWrapper() { someCplusplusFunction(); } 在someCpluplusfunction.cpp中 : void someCplusplusFunction() { printf(“Inside the C++ function”); } 在main.swift中 : someCFunction(); aWrapper(); 在Bridging-Header.h中 : #import “someCFunction.h” #import “someCplusplusFunction.h” 我发现这个答案非常有用,但我仍然无法使其发挥作用。 你能指出我正确的方向吗? 谢谢!

Swift数组与C的互操作性?

如何在C数组中传递或复制数据,例如 float foo[1024]; 在C和Swift函数之间使用固定大小的数组,例如声明的 let foo = Float[](count: 1024, repeatedValue: 0.0) ?

Swift使用c结构

对不起标题,我找不到用几句话来形容我的问题。 我已经知道swift可以使用c编写的struct。 例如 在Bridging-Header.h中 typedef struct { int x; int y; } Pointer; 然后我可以直接使用Pointer。 但就我而言,我有一个用C语言编写的库。有许多带有隐藏工具的结构。 例如: 在Briding-Header.h中 typedef struct Pointer Pointer; 我不能再使用Pointer了,得到了未知类型。 在我的库中,Pointer用作 create_pointer(Pointer **pointer); 任何帮助表示赞赏! PS我没有定义struct Pointer的.h文件。 关于Pointer的所有细节都是隐藏,例如通过函数访问它们 int getx(Pointer *pointer); 这是我的完整测试代码: user_input.c #include #include “user_input.h” struct Pointer { int x; int y; }; void get_user_input(int *user_input) { scanf(“%i”, user_input); } void init_pointer(Pointer *point) […]

为什么我不能将可选的Swift String传递给允许NULL指针的C函数?

我有一个处理C字符串的C函数。 该函数实际上允许字符串为NULL指针。 声明如下: size_t embeddedSize ( const char *_Nullable string ); 在C中使用这样的函数没问题: size_t s1 = embeddedSize(“Test”); size_t s2 = embeddedSize(NULL); // s2 will be 0 现在我正试图从Swift中使用它。 以下代码有效 let s1 = embeddedSize(“Test”) let s2 = embeddedSize(nil) // s2 will be 0 但什么不起作用是传递一个可选的字符串! 这段代码不会编译: let someString: String? = “Some String” let s2 = embeddedSize(someString) 编译器抛出一个关于可选的未被解包的错误,Xcode询问我是否可能忘记添加! 还是? 。 […]

如何正确处理UnsafeMutablePointer

我有点困惑。 什么时候我必须免费拨打电话和/ devalloc? 我正在研究一个学习核心音频的简短代码片段。 我想如果我调用UnsafeMutablePointer.alloc(size)那么我应该调用destroy & dealloc 。 但是如果我使用malloc()或calloc()我应该调用free() 。 在这个来自Learning Core Audio的示例中,以下代码片段让我想知道: var asbds = UnsafeMutablePointer.alloc(Int(infoSize)) audioErr = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AvailableStreamDescriptionsForFormat, UInt32(sizeof(fileTypeAndFormat.dynamicType)), &fileTypeAndFormat, &infoSize, asbds) 在这里我使用alloc 。 调用释放内存是free的。 free(asbds) 但为什么不呢 asbds.destroy(Int(infoSize)) asbds.dealloc(Int(infoSize)) 我希望遵循规则。 我会感激任何帮助,因为这让我头晕目眩。 文档说我负责销毁和dealloc,以便部分清晰,但是以哪种方式?

在Swift中向C函数表示NULL函数指针

考虑一下私有但尚未记录的Cocoa C函数_NSLogCStringFunction()和_NSSetLogCStringFunction() 。 _NSLogCStringFunction()返回一个函数指针,指向NSLog()幕后的Objective-C运行时使用的C函数, _NSSetLogCStringFunction()允许开发人员指定自己的C函数进行日志记录。 有关这两个函数的更多信息可以在此Stack Overflow问题和此WebObjects支持文章中找到 。 在C中,我可以传入NULL函数指针到_NSSetLogCStringFunction() : extern void _NSSetLogCStringFunction(void(*)(const char*, unsigned, BOOL)); _NSSetLogCStringFunction(NULL); // valid 但是,当我尝试在纯Swift中执行此操作时,我遇到了一些问题: /// Represents the C function signature used under-the-hood by NSLog typealias NSLogCStringFunc = (UnsafePointer, UInt32, Bool) -> Void /// Sets the C function used by NSLog @_silgen_name(“_NSSetLogCStringFunction”) func _NSSetLogCStringFunction(_: NSLogCStringFunc) -> Void _NSSetLogCStringFunction(nil) // Error: […]

Swift中的免费C-malloc()内存?

我正在使用Swift编译器的Bridging Headerfunction来调用使用malloc()分配内存的C函数。 然后它返回一个指向该内存的指针。 函数原型是这样的: char *the_function(const char *); 在Swift中,我使用它: var ret = the_function((“something” as NSString).UTF8String) let val = String.fromCString(ret)! 原谅我对Swift的无知,但通常在C中,如果the_function()是malloc的内存并返回它,其他人需要在某个时候释放()它。 这是由Swift以某种方式处理还是我在这个例子中泄漏内存? 提前致谢。

如何在Swift中实现AudioServicesSystemSoundCompletionProc?

我正在尝试并且未能使用Xcode中的Swift为AudioServicesAddSystemSoundCompletion中的参数创建AudioServicesSystemSoundCompletionProc的实例。 这是我到目前为止所得到的 func completionCallback(ssID:SystemSoundID,clientData:UnsafeMutablePointer) -> Void { } var foo:(ssID:SystemSoundID,clientData:UnsafeMutablePointer) -> Void = completionCallback; AudioServicesAddSystemSoundCompletion(soundID, nil, nil, foo, nil); 我在一些指南的帮助下写了这篇文章,解释了如何在Swift中编写等效的C函数指针,但这引发了这个错误: ‘(ssID: SystemSoundID, clientData: UnsafeMutablePointer) -> Void’ is not convertible to ‘AudioServicesSystemSoundCompletionProc’ 文档显示了Objective-C声明: typedef void (*AudioServicesSystemSoundCompletionProc) ( SystemSoundID ssID, void *clientData ); 这是使用Xcode时显示的声明: typealias AudioServicesSystemSoundCompletionProc = CFunctionPointer<((SystemSoundID, UnsafeMutablePointer) -> Void)> 我不确定如何在Swift中正确实现AudioServicesSystemSoundCompletionProc。

Swift转换C的uint64_t与使用自己的UInt64类型不同

我正在将应用程序从(Objective-)C移植到Swift,但必须使用用C编写的第三方框架。有一些不兼容的东西,比如typedef,它们被解释为Int,但必须传递给框架的function如UInts等。 因此,为了避免整个Swift应用程序中的常量转换操作,我决定将C头文件传输到Swift,所有类型的II都需要它们在一个地方。 我能够转移几乎所有的东西,并克服了很多障碍,但这一个: C头定义了一个包含uint64_t变量的结构。 此结构用于将数据作为指针传输到回调函数。 回调函数将void指针作为参数,我必须使用UnsafeMutablePointer操作将其转换为结构的类型(或者如果合适的话,还是标题的另一个结构)。 只要我使用导入时由Swift自动转换的C头中的原始结构,所有的转换和内存访问都可以正常工作。 但是,在Swift中手动复制结构并不是“字节匹配”。 让我向您展示一个这种情况的简化示例: 在CApiHeader.h文件中有类似的东西 typedef struct{ uint32_t var01; uint64_t var02; uint8_t arr[2]; }MyStruct, *MyStructPtr; 根据我的理解,这里应该是Swift的等价物 struct MyStruct{ var01: UInt32 var02: UInt64 arr: (UInt8, UInt8) } 或者也应该有效的是这种元组符号 typealias MyStruct = ( var01: UInt32, var02: UInt64, arr: (UInt8, UInt8) ) 这可以正常工作,但不是只有UInt64类型。 好的,那会发生什么? 将指针转换为我自己的Swift MyStruct实现之一,从UInt64字段开始,将孔数据移位2个字节。 因此,在此示例中,两个arr字段都不在正确的位置,但在UInt64位内,其数量应为64。 所以它的接缝是UInt64字段只有48位。 这符合我的观察,如果我用这个替代品替换UIn64变量 struct MyStruct{ var01: UInt32 […]

在Swift中使用CFNotificationCallback,或在Swift中使用@convention(c)块

我正在尝试使用(现在私有的) CTTelephonyCenterAddObserver C函数和CFNotificationCallback回调块来侦听CoreTelephony通知。 我的桥接头(外部私有C函数): #include #if __cplusplus extern “C” { #endif #pragma mark – API /* This API is a mimic of CFNotificationCenter. */ CFNotificationCenterRef CTTelephonyCenterGetDefault(); void CTTelephonyCenterAddObserver(CFNotificationCenterRef center, const void *observer, CFNotificationCallback callBack, CFStringRef name, const void *object, CFNotificationSuspensionBehavior suspensionBehavior); void CTTelephonyCenterRemoveObserver(CFNotificationCenterRef center, const void *observer, CFStringRef name, const void *object); void CTTelephonyCenterRemoveEveryObserver(CFNotificationCenterRef center, […]