我知道可以使用 objc_msgSend 来使用 Objective C 代码,我相信,手动运行 Objective C 运行时但是当我运行此代码时,我收到引用 NSString 的错误(即使我从未使用它)以及其他未使用的类.
来自 xcode 的错误
我在它上面有我试图“模仿”的目标 C 代码(注释掉)。
#include <Foundation/Foundation.h> /*Added suggestion by answer, same errors*/
#include <AppKit/AppKit.h>
int main()
{
// convert objective c into c code
/*
NSAlert *alert = [[NSAlert alloc] init];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert setMessageText:@"Hello World"];
[alert setInformativeText:@"Hello World"];
[alert runModal];
*/
id alert = objc_msgSend(objc_msgSend(objc_getClass("NSAlert"), sel_registerName("alloc")), sel_registerName("init"));
objc_msgSend(alert, sel_getUid("setAlertStyle:"), NSInformationalAlertStyle);
objc_msgSend(alert, sel_getUid("setMessageText:"), CFSTR("Hello World!"));
objc_msgSend(alert, sel_getUid("setInformativeText:"), CFSTR("Hello World!"));
objc_msgSend(alert, sel_getUid("runModal"));
}