从笔尖Objective-C的清醒调用C函数 [英] Calling C function in Objective-C Awake from Nib

查看:97
本文介绍了从笔尖Objective-C的清醒调用C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不实施在Objective-C类C函数,通常会被调用与 INT主(在它自己的文件)。由于我不熟悉拼接code我需要知道如何把它无论是在 awakefromnib 的applicationDidFinishLaunching 从可可段内。或者,如果有一个更好的方法来做到这一点,我听着。

这里有没有得到太多的技术的例子:

  //进口可可#进口AppDelegate.h
...// C Inlcudes
#包括LT&;&stdio.h中GT;//(INT主不得不更改为其他明显)INT DOS(){
的printf(我是一个C程序\\ n);
}//(回可可)@implementation的AppDelegate@synthesize窗口; - (无效)的applicationDidFinishLaunching:(NSNotification *)aNotification
{// [这是我需要我上面的C函数来执行]


解决方案

这就是所谓的完全一样,你从C会,你可以调用任何你喜欢的C函数:

  //进口可可#进口AppDelegate.h
...// C Inlcudes
#包括LT&;&stdio.h中GT;//(INT主不得不更改为其他明显)INT DOS(为const char *文件名)
{
    的printf(我通过'%s'的\\ n,文件名);
}//(回可可)@implementation的AppDelegate@synthesize窗口; - (无效)的applicationDidFinishLaunching:(NSNotification *)aNotification
{
    DOS(/路径/要/一些/文件);
}

I had to implement a C function in an Objective-C class that normally would get called with int main (in it's own file). Since I'm unfamiliar with splicing code I need to know how to have it called upon in either the awakefromnib or applicationDidFinishLaunching from within the Cocoa section. Or if there's a much better way to do this I'm listening.

Here's an example without getting too technical:

// 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]

解决方案

It's called just the same as you would from C and you can call any C function you like:

// 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的清醒调用C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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