方法调配 - 如何确保方法在调用之前进行调配

发布于 2024-10-11 21:41:13 字数 240 浏览 9 评论 0原文

我正在使用 SIMBL 方法调配第三方应用程序创建 NSMenuItems,但 50/50 的时间菜单项是在我的方法调配初始化之前创建的。

有什么干净的方法可以确保我的搅拌始终是第一位的?我想我可以调整 applicationDidFinishLaunching: 并在那里继续我的调整。但我担心我会在那里遇到同样的错误,在我的实际 swizzle 到位之前将调用 applicationDidFinishLaunching 。

约翰

I'm method swizzling a third party applications creation of NSMenuItems with SIMBL, but 50/50 of the time the menu-items are created before my method swizzling is initialized.

What is a clean way to make sure my swizzling always comes first? I guess I could swizzle applicationDidFinishLaunching: and continue my swizzling there. But I'm afraid I'm going to run in to the same error there, where applicationDidFinishLaunching will be called before my actual swizzle is in place.

John

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

來不及說愛妳 2024-10-18 21:41:13

您希望在加载库后立即进行混合。您可以通过 +initialize+load 或构造函数来完成此操作。

@bbum 的对此问题的回答提供了更多信息,以及他的一篇博客文章使用这些特殊类方法的注意事项。

(我故意不质疑你所做的事情是否明智;))

You'd want the swizzle to happen as soon as the libraries are loaded. You can do that via +initialize, +load, or a constructor function.

@bbum's answer to this question has a bit more information, along with one of his blog posts on the caveats of using these special class methods.

(And I'm purposely not questioning the wisdom of what you're doing ;) )

開玄 2024-10-18 21:41:13

您可以像这样使用构造函数:

__attribute__((constructor)) static void do_the_swizzles()
{
    // Do all your swizzling here.
}

来自 GCC 文档

constructor 属性导致函数被调用
在执行进入 main() 之前自动执行。

注意:虽然这最初来自于GCC,但它也适用于LLVM

You can use constructor functions like this:

__attribute__((constructor)) static void do_the_swizzles()
{
    // Do all your swizzling here.
}

From GCC documentation:

The constructor attribute causes the function to be called
automatically before execution enters main().

Note: Although this is originally from GCC, it also works in LLVM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文