从调用雨燕C函数 [英] Calling C function from Swift

查看:146
本文介绍了从调用雨燕C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打电话从斯威夫特C函数,但我不知道如何定义变量来传递参数。

函数C是:

  DBFGetFieldInfo(DBFHandle psDBF,诠释iField,字符* pszFieldName,为int * pnWidth,为int * pnDecimals);

主要的问题是 pszFieldName pnWidth pnDecimals INOUT参数。我试图做:

  VAR DBF:DBFHandle = DBFOpen(pszPath,RB)
VAR FNAME:[CCHAR] = []
VAR fieldWidth:的Int32 = 0
让fieldDecimals:的Int32 = 0让字段信息:DBFFieldType = DBFGetFieldInfo(DBF,我,FNAME,&安培; fieldWidth,&安培; fieldDecimals)

但它给我一个错误

 无法援引DBFGetFieldInfo与类型的参数列表(DBFHandle,的Int32 [CCHAR],INOUT的Int32,INOUT的Int32)'
预计类型的参数列表(DBFHandle,的Int32,UnsafeMutablePointer< INT8>中UnsafeMutablePointer<的Int32>中UnsafeMutablePointer<&的Int32 GT;)

任何想法?


解决方案

  UnsafeMutablePointer< INT8>中UnsafeMutablePointer<的Int32>中UnsafeMutablePointer<的Int32>

您需要将您的变量转换成该方法签名所需的适当类型。

C语法:


  • const型*

  • 键入*

斯威夫特语法:


  • UnsafePointer

  • UnsafeMutablePointer

这是苹果公司在其使用SWIFT与可可和Objective-C基准<一个覆盖href=\"https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html\"相对=nofollow>设在这里。

I am trying to call a C function from Swift , but I do not know exactly how to define variables to pass parameters.

The function c is:

DBFGetFieldInfo( DBFHandle psDBF, int iField, char * pszFieldName, int * pnWidth, int * pnDecimals );

The main problem is pszFieldName, pnWidth and pnDecimals inout parameters. I tried made ​​:

var dbf:DBFHandle = DBFOpen(pszPath, "rb")
var fName:[CChar] = [] 
var fieldWidth:Int32 = 0
let fieldDecimals:Int32 = 0

let fieldInfo:DBFFieldType = DBFGetFieldInfo(dbf, i, fName, &fieldWidth, &fieldDecimals)

but it gives me an error

Cannot invoke 'DBFGetFieldInfo' with an argument list of type '(DBFHandle, Int32, [CChar], inout Int32, inout Int32)'
Expected an argument list of type '(DBFHandle, Int32, UnsafeMutablePointer<Int8>, UnsafeMutablePointer<Int32>, UnsafeMutablePointer<Int32>)'

Any ideas?

解决方案

UnsafeMutablePointer<Int8>, UnsafeMutablePointer<Int32>, UnsafeMutablePointer<Int32>

You need to convert your variables to the appropriate types required by the method signature.

C Syntax:

  • const Type *
  • Type *

Swift Syntax:

  • UnsafePointer
  • UnsafeMutablePointer

This is covered by Apple in their Using Swift with Cocoa and Objective-C reference located here.

这篇关于从调用雨燕C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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