我正在尝试在纯 C 中获得
NSHomeDirectory()
#include <CoreFoundation/CFBundle.h>
#include <CoreFoundation/CoreFoundation.h>
void testFunc() {
printf(NSHomeDirectory());
}
需要将
UTF8String
消息发送给NSString
返回的NSHomeDirectory()
对象,获取C字符串。要在纯 C 中执行此操作(已测试):
#include <objc/runtime.h>
#include <objc/message.h>
void *NSHomeDirectory();
void testFunc() {
puts(((const char *(*)(void *, SEL))objc_msgSend)(NSHomeDirectory(), sel_getUid("UTF8String")));
// Also use puts() instead of printf() here
}
注:使用
-framework Cocoa
编译