函数调用 - 目标C

时间:2011-05-03 05:58:51

标签: c++ objective-c

我是Objective-C的新手。我正在将C ++代码移植到Objective-C中。我有 这个函数调用eventLog() samples.cpp我已宣布这一点 samples.h中的函数。我有它 EventLog.cpp上的定义 包括samples.h头文件。

samples.h

class samples
{
 declaration;
}

samples.cpp

#include "samples.h"
void samples::buttonClick()
{
 eventLog();
}

EventLog.h

#include "samples.h"

EventLog.cpp

void samples::eventLog()
{
 //definition;
} 

这是Objective-C的等价物吗?

samples.h

- (void)eventLog;

samples.m

samples* a = [[samples alloc]init];
[a eventLog];

EventLog.h

#import "samples.h"

EventLog.m

-(void)eventLog
{
 //Definition;
}

2 个答案:

答案 0 :(得分:1)

看起来不错,除非你在做

#import <Foundation/Foundation.h>

@interface EventLog : NSObject {

}
- (void)eventLog;
@end

#import "EventLog.h" 
#import "Samples.h"

@implementation EventLog

- (void)eventLog
{
     //definition
}

@end

?如果您没有“class wrappers”类,那么您一定会遇到问题。此外,您的文件非常棘手。如果samples.h实施eventLog,则不应在EventLog.m中实施。那将是samples.m的工作。有关可能的结构,请参阅上面的示例。

答案 1 :(得分:0)

不,这是错的...... 使用以下行:

在sample.m中创建EventLog.m的实例
EventLog *obj = [[EventLog alloc] init]; 

Then, use [EventLog eventLog];