ksnowlv

回顾过去,总结以往;立足现在,铭记当下;技术为主,笔记而已.

Swift Call C

| Comments

如何在swift调整c代码呢?

swift通过工程的桥接文件,调用oc或c的相关代码!!!

1.创建c文件:test.htest.c

test.h内容如下:

1
2
3
4
5
6
7
8
#ifndef test_h
#define test_h

#include <stdio.h>

void showValue(int *value);

#endif /* test_h */

test.c内容如下

1
2
3
4
5
6
7
#include "test.h"

void showValue(int *value) {
    printf("old value = %d\n",*value);
    *value = *value + 1;
    printf("new value = %d\n",*value);
}

2.在桥接文件中,加入test.h引用:#include "test.h"

3.swift中调用

1
2
3
   var value: Int32 = 0
   showValue(&value)

结果显而易见:

1
2
old value = 0
new value = 1

Comments

comments powered by Disqus
Included file 'custom/after_footer.html' not found in _includes directory