swift - 快速将结构数据从类发送到实例
问题描述
我用 C 语言制作了一个静态库。该库的功能如下。
- 将回调函数点保存在全局变量中
- 回调函数有一个struct结构的参数。
- 库通过在特定时刻调用回调函数将结构数据传输给库用户。
其来源是:
- 静态库源
// ---------------------------------------------
// Static Library
// ---------------------------------------------
typedef struct
{
unsigned long m_Data1;
unsigned long m_Data2;
unsigned long m_Data3;
} SEND_DATA;
//
typedef int (*ReportCallbackProc)(SEND_DATA *pSendData);
//
static ReportCallbackProc gReportFunc = NULL;
static Period = 0;
// Set callback function pointer to gloval variable
int SetReport(ReportCallbackProc func, int period)
{
gReportFunc = func;
Period = period;
return(1);
}
// Send data to Application of library
void Report()
{
.............
SEND_DATA sendData;
sendData.m_Data1 = 1;
sendData.m_Data2 = 2;
sendData.m_Data3 = 3;
int rc = gReportFunc(&sendData);
if (rc != 1)
{
printf("gRealtimeReportFunc failed: status=[%d]\n", rc);
}
.....
}
- 这个库的应用
// ---------------------------------------------
// Application of Static Library
// ---------------------------------------------
void handle_report (SEND_DATA *pSendData)
{
printf("Report called");
printf("m_Data1 = %ld ", pSendData->m_Data1);
printf("m_Data2 = %ld ", pSendData->m_Data2);
printf("m_Data3 = %ld ", pSendData->m_Data3);
}
void main()
{
SetReport (&handle_report, period);
}
我想迅速制作上述来源。目前,我正在尝试在学习 swift 时实施。但是,这不是一个简单的问题,需要帮助。我尝试创建和实现闭包用法和类,但这绝非易事。
你能提供类似的快速代码吗?你能告诉我如何实现它吗?
解决方案
推荐阅读
- ios - iOS:这个 NSPersistentStoreCoordinator 没有持久存储 => 如何检查应用程序中的 sqlite 数据库是否与模型对应?
- codenameone - 如何将多个浏览组件放在一个代号容器中?
- ios - iOS中的本地通知(目标C)
- mojolicious - 带有正则表达式的 Mojolicious Lite 路由
- java - 无法在 android studio 中解析导入 javafx.util.Pair
- node.js - 在graphql nodejs express app中调用方法时出错
- sql - 使用 CTE 创建数据子集
- devops - 升级后地图中的 zabbix-fronend-php 出现问题
- ios - 如何将 AutoLayout 约束的常量设置为超级视图的百分比?
- ios - Xcode - 可以在模拟器上但不能在设备上运行应用程序 - 错误:失败,退出代码 1