来自纯C ++的NSUserNotification

时间:2017-12-11 20:23:15

标签: c++ objective-c nsusernotification

我正在尝试从纯C ++发布UserNotification。我的代码基于这个SO答案:https://stackoverflow.com/a/14083212/5548305

现在我无法为对象属性设置值。我确信有些东西我不知道了。我试图利用setValue:forKey,但我找不到任何关于它的文档。我搜索了objc-runtime.h和obj.h,但找不到任何跳出来的东西。有没有人尝试过这个?

#include <CoreFoundation/CoreFoundation.h>
#include <objc/objc.h>
#include <objc/objc-runtime.h>
#include <iostream>    

int main(int argc, char** argv)
{    

    id app = NULL;


    id notif = (id)objc_getClass("NSUserNotification");
    notif = objc_msgSend(notif, sel_registerName("alloc"));
    notif = objc_msgSend(notif, sel_registerName("init"));
    notif = sel_setValue(CFSTR("title"), id("title"));      <-This line here


    objc_msgSend(pool, sel_registerName("release"));
    return 0;
}

2 个答案:

答案 0 :(得分:1)

该代码是否实际编译? Apple近年来关于Objective-C运行时的文档变得更加糟糕,但我不记得并且找不到sel_setValue的任何提及。

-setValue:forKey:只是一个常规的消息,所以我希望更像是:

objc_msgSend(notif, sel_registerName("setValue:forKey:"), 
             CFSTR("title"), CFSTR("title"))

基于objc_msgSend采用可变数量的参数,前两个是目标对象和选择器,其余是该方法的各种其他参数。目标键名称作为字符串提供,它们在内部映射到属性,尝试了几种不同的组合,在这种情况下,属性也希望保留一个字符串。

setValue:forKey:的声明返回类型为void,因此无法捕获返回值。

答案 1 :(得分:0)

如果将来有人试图实现这一目标,我最终会让这个工作并在github上制作一个项目,展示如何在纯C中发布NSUserNotification。

https://github.com/jslegendre/NS-C-UserNotification