需要帮助把一个C函数成Objective C的方法 [英] Need help turning a C function into an Objective C method

查看:138
本文介绍了需要帮助把一个C函数成Objective C的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C语言编写一个函数,它是相当大的,目前仅在C文件中的main()函数运行时,我一直用它来测试它。下面是它的声明

I have a function written in C that is rather large and currently is only running in the main() function of a C file while I have been using to test it out. Here is it's declaration

void getData(const char* encodedBuffer, int user);

如果你想看到的getData 的内容这里对于引擎收录吧。

If you would like to see the contents of getData Here is the pastebin for it.

截至目前,我只是路过的EN codedBuffer我将有一个过得去的的getData 函数更新一个全局变量。

As of now I'm just passing in encodedBuffer which I will have a global variable that is getting updated by the getData function.

我想知道正确的方法是把这样一个基于C函数成Objective-C的协议方法是什么。现在,我设置它像这样

I would like to know what the proper way is to turn a C based function like this into a Objective-C protocol method. Right now I'm setting it up like this

iDriver.h(我的接口/协议)

iDriver.h (my interface/protocol)

#import <Foundation/Foundation.h>

@protocol iDriver <NSObject>
-(void)getData;
-(void)cancel;

@end

DriverOne.h(类真正实现协议方法)

DriverOne.h (Class that actually implements the protocol method)

#import <Foundation/Foundation.h>
#import "iDriver.h"

@interface DriverOne : NSObject <iDriver>

@end

DriverOne.m

DriverOne.m

#import "DriverOne.h"

@implementation DriverOne

enum Status getData(char* encodedBuffer, int user)
{
    // Copy C code which I showed in the link earlier
    // to above into this method. I will want it to return a status
    // and populate a global variable with the data.
}

我的想法是,我真的不应该碰上这样做,因为Objective-C的是C的一个超集任何问题,而只是复制C $ C $ç成的方法一样,是有问题的?

My thought is that I shouldn't really run into any issues doing this since Objective-C is a superset of C but just copying the C code into a method like that be problematic?

推荐答案

在移植的C函数到ObjC方法,它只是一个调整的签名。在你的情况,你会改变:

When porting a C function to an ObjC method, it's just a tweak to the signature. In your case, you'll change:

enum Status getData(char* encodedBuffer, int user)

喜欢的东西:

- (enum Status) getData: (char*) encodedBuffer user: (int) user

Objective C的方法是用始终表示 - 开头(类方法开始与 + )。如果要指定类型,你把括号(否则它假定为键入 ID 这是通用的对象)。你必须keywordize你的函数名。一个关键字为每一个参数。

Objective C methods are always denoted with a - at the beginning (class methods start with a +). If you want to specify the type, you put in parens (otherwise it's assumed to be type id which is generic for "object"). And you have to keywordize your function name. One keyword for each argument.

然后就像C,你可以复制该行粘贴到你的头文件:

Then just like C, you can just copy paste that line into your header file:

@protocol iDriver <NSObject>
- (enum Status) getData: (char*) encodedBuffer user: (int) user;
@end

在刚胆量复制方法中。

目前尚不清楚你背后OO和实例的思想多么熟悉,但使用它可能看起来像:

It's not clear how familiar you are with the ideas behind OO and instances, but using it might look something like:

DriverOne *driverOneObject = [[DriverOne alloc] init];
char *buffer = malloc(YOUR_BUFFER_SIZE);
enumStatus status = [driverObject getData: buffer user: SomeUserThing];

一圈下来,有没有你想要这个被物化(变成对象)的一个原因?我所有的大量对象的自己,但ObjectiveC的特点之一,就是它是C的超集,你可以用你的功能是从C原型。你不必把它包起来了在ObjectiveC对象,除非你看到一个优势,这样做。

Coming full circle, is there a reason you want this to be reified (turned into objects)? I'm all for lots of objects myself, but one of the features of ObjectiveC, is that it IS a superset of C. You can just use your function as is from your C prototype. You don't have to wrap it up in an ObjectiveC object, unless you see an advantage to doing so.

这篇关于需要帮助把一个C函数成Objective C的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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