函数调用-Objective C

Function calling -Objective C

本文关键字:-Objective 函数调用      更新时间:2023-10-16

我是Objective-C的新手。我正在将c++代码移植到Objective-C中。我有这个函数调用eventLog() insamples.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;
}

看起来不错,除了你在做你的

#import <Foundation/Foundation.h>
@interface EventLog : NSObject {
}
- (void)eventLog;
@end

#import "EventLog.h" 
#import "Samples.h"
@implementation EventLog
- (void)eventLog
{
     //definition
}
@end

?如果没有适当的类"包装器",就必然会遇到问题。还有,你的文件很乱。如果samples.h实现了eventLog,你就不应该在EventLog.m中实现它。这将是samples.m的工作。

参考我上面的例子。

不,这是错误的…
创建一个EventLog实例。样本中的M。M,使用以下行:

EventLog *obj = [[EventLog alloc] init]; 
Then, use [EventLog eventLog];