Swift-如何使用malloc? [英] Swift - how to use malloc?

查看:114
本文介绍了Swift-如何使用malloc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Objective-C应用程序转换为Swift,但我不知道如何实现malloc.

是否可以在Swift中使用它?

谢谢

解决方案

在使用Swift中的ObjC或C时,您需要实现桥接标头.然后,在您的Swift应用程序/模块中就可以使用桥接头导出的功能.概述,请参见此处. >

如果只需要在C端调用一些代码",那么从C导出的函数基本上只是Swift的包装器.但是,如果您需要与从这些函数返回的数据进行交互(尤其是如果使用malloc而不是简单的原语),则Swift可以使用许多与C相关的类型(请参见 解决方案

You need to implement a bridging header when you use ObjC or C from Swift. The functions exported by your bridging header are then available in your Swift app/module. See here, for the overview.

If you just need to "call some code" on the C-side, then the functions exported from C are basically just wrappers for Swift. However, if you need to interact with the data returned from those functions -- especially if malloc'd and not a simple primitive -- Swift has a number of C related types ready for your use (see here for specifics).

Furthermore, if you're trying to wrap or interact with C++ code, you can't directly do so from Swift. You have to setup an initial interface with ObjC or C for the C++ code, and then bridge that to Swift. Not really fun at all, but thankfully it's not as common a use case as bridging ObjC (primarily) or C.

... and for what it's worth, unless you need low level Core Audio for some reason (granted, like porting an app you already have), AVAudioEngine (iOS8+) is so much simpler for any applicable use case than Core Audio, and is readily available in Swift.

这篇关于Swift-如何使用malloc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆