在Objective-C类混合使用C函数 [英] Mixing C functions in an Objective-C class
问题描述
我写一个Objective-C类,但它使用C语言编写的一个API,这主要是罚款,混合C调用与Objective-C的调用原因一些问题。
然而,API调用的一个需要回调方法(例):
成功= CFHostSetClient(主机,MyCFHostClientCallBack,&安培;背景);
其中, MyCFHostClientCallBack
是这样定义的C函数:
静态无效MyCFHostClientCallBack(CFHostRef主机,CFHostInfoType所属类别,常量CFStreamError *错误,无效*信息);
- 可以/我如何调用一个Objective-C的方法代替呢?
- 可以/我应该用我的Objective-C的混合调用C函数?
- 如何混合使用Objective-C的方法C函数?
混合使用C和Objective-C的方法和功能是可能的,这里是一个使用iPhone应用程序中的SQLite的API一个简单的例子:(的course网站)
<一个href=\"http://stanford.edu/class/cs193p/cgi-bin/drupal/system/files/sample_$c$c/09_MySQLiteTableView.zip\">http://stanford.edu/class/cs193p/cgi-bin/drupal/system/files/sample_$c$c/09_MySQLiteTableView.zip
C函数需要外被宣布为 @implementation
在一个Objective-C(.M)文件。
INT MyCFunction(INT NUM,无效*数据)
{
// code这里...
}@implementation - (无效)MyObjectiveCMethod:(INT)号withData:(NSData的*)数据
{
// code在这里
}@结束
由于C函数是外面的 @implementation
它不能调用像
[个体经营DoSomething的]
和具有高德的访问权限。
这可只要回调函数接收工作围绕一个 USERINFO
或背景
类型参数,一般类型的无效*
。这可以用于任何Objective-C的对象发送到C函数。
如在<一href=\"http://stanford.edu/class/cs193p/cgi-bin/drupal/system/files/sample_$c$c/09_MySQLiteTableView.zip\">sample code ,这可以通过正常的Objective-C的进行操作。
此外,请阅读这样的回答:<一href=\"http://stackoverflow.com/questions/801976/mixing-c-functions-in-an-objective-c-class/802059#802059\">Mixing在Objective-C类 C函数
I am writing an Objective-C class but it uses an API written in C. This is mostly fine as mixing C calls with Objective-C calls causes few problems.
However one of the API call requires a call back method (example):
success = CFHostSetClient(host, MyCFHostClientCallBack, &context);
Where MyCFHostClientCallBack
is a C function defined like this:
static void MyCFHostClientCallBack(CFHostRef host, CFHostInfoType typeInfo, const CFStreamError *error, void *info);
- Can/How do I call an Objective-C method in place of this?
- Can/Should I mix C functions with my Objective-C calls?
- How do I mix C functions with Objective-C methods?
Mixing C and Objective-C methods and function is possible, here is a simple example that uses the SQLite API within an iPhone App: (course site)
http://stanford.edu/class/cs193p/cgi-bin/drupal/system/files/sample_code/09_MySQLiteTableView.zip
C functions need to be declared outside of the @implementation
in an Objective-C (.m) file.
int MyCFunction(int num, void *data)
{
//code here...
}
@implementation
- (void)MyObjectiveCMethod:(int)number withData:(NSData *)data
{
//code here
}
@end
Because the C function is outside of the @implementation
it cannot call methods like
[self doSomething]
and has no access to ivars.
This can be worked around as long as the call-back function takes a userInfo
or context
type parameter, normally of type void*
. This can be used to send any Objective-C object to the C function.
As in the sample code, this can be manipulated with normal Objective-C operations.
In addition please read this answer: Mixing C functions in an Objective-C class
这篇关于在Objective-C类混合使用C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!