我必须在 Objective-C 类中实现一个 C 函数,该类通常会使用 int main
(在它自己的文件中)调用。由于我不熟悉拼接代码,因此我需要知道如何在 Cocoa 部分的 awakefromnib
或 applicationDidFinishLaunching
中调用它。或者,如果有更好的方法来做到这一点,我正在倾听。
这是一个不太技术性的例子:
// Cocoa Imports
#import "AppDelegate.h"
...
// C Inlcudes
#include <stdio.h>
// (int main had to change to something else obviously)
int dos () {
printf ("I'm a C program\n");
}
// (back to cocoa)
@implementation AppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
//[This is where I need my C function above to execute]
最佳答案
它的调用方式与在 C 中的调用方式相同,您可以调用任何您喜欢的 C 函数:
// Cocoa Imports
#import "AppDelegate.h"
...
// C Inlcudes
#include <stdio.h>
// (int main had to change to something else obviously)
int dos (const char *filename)
{
printf ("I was passed '%s'\n", filename);
}
// (back to cocoa)
@implementation AppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
dos("/path/to/some/file");
}
关于objective-c - 从 Nib 唤醒在 Objective-C 中调用 C 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9339843/