【发布时间】:2014-03-11 19:32:54
【问题描述】:
我有一个用 C 编写的函数,它相当大,目前只在 C 文件的 main() 函数中运行,而我一直在使用它来测试它。这是它的声明
void getData(const char* encodedBuffer, int user);
如果你想查看getData的内容Here是它的pastebin。
到目前为止,我只是传入了 encodedBuffer,我将拥有一个由 getData 函数更新的全局变量。
我想知道将这样的基于 C 的函数转换为 Objective-C 协议方法的正确方法是什么。现在我是这样设置的
iDriver.h(我的接口/协议)
#import <Foundation/Foundation.h>
@protocol iDriver <NSObject>
-(void)getData;
-(void)cancel;
@end
DriverOne.h(实际实现协议方法的类)
#import <Foundation/Foundation.h>
#import "iDriver.h"
@interface DriverOne : NSObject <iDriver>
@end
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 代码复制到这样的方法中会有问题吗?
【问题讨论】:
-
stackoverflow.com/questions/19366134/… 我不认为你应该遇到任何问题,但话又说回来,尝试不会有什么坏处。
-
为什么需要协议?为什么不在
DriverOne上使用getData和cancel方法? -
@TravisGriggs 因为不久之后我将不得不添加另一个也将使用 getData 的驱动程序。我还将更改 getData 以返回一个名为 status 的枚举类型的结构。我真的只是认为最好有一个可以使用的界面,但我对 C 和 Obj-C 都是新手,所以我可能是错的
标签: objective-c c cocoa oop