Objective-C:加载“库”使用反射

发布于 2024-11-30 09:51:19 字数 378 浏览 0 评论 0原文

我的任务是使用 XCode for OSX 创建一个应用程序。该应用程序需要能够加载并运行动态确定的单独“模块”(即,一个用户可能购买了模块 1 和 2,而用户 2 可能购买了模块 3 和 6——只有购买的模块才应该“跑步”)。

在 C# 中,我将创建一个“库”项目(仅编译为 DLL)。当用户购买模块时,我将提供适当的 DLL 文件,然后我的应用程序将使用反射查找并加载/运行 DLL。

在 XCode 中与此等效的是什么?我可以创建一个“库”,然后使用反射加载它吗?请记住,应用程序无法预先了解模块,因为在某些情况下,用户甚至不拥有模块文件。我看到了各种选项,例如“Cocoa Framework”和“Cocoa Library”以及“C/C++ Library”。每个人都做什么?是否可以做我需要的事情?

I'm tasked with creating an application using XCode for OSX. This application needs to be able to load and run separate "modules" which will be determined dynamically (i.e., one user may have purchased modules 1 & 2, while user 2 would have purchased modules 3 and 6 -- only purchased modules should "run").

In C#, I would create a "library" project (that compiles to just a DLL). When the user purchases a module, I'd supply the appropriate DLL files and then my app would look for and load/run the DLL using reflection.

What would be the equivalent to this in XCode? Can I create a "library" and then load it using reflection? Keeping in mind that the app can't have prior knowledge of the module since in some cases, the user wouldn't even own the module files. I see various options such as "Cocoa Framework" and "Cocoa Library" as well as "C/C++ Library." What does each do and would any work to do what I need?

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

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

发布评论

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

评论(1

醉酒的小男人 2024-12-07 09:51:19

您可以创建一个捆绑包。 Xcode 有这方面的模板(称为“Loadable Bundle”,图标是乐高积木)。您通常使用 NSBundleload 方法加载包。

加载它的一个例子是:

- (BOOL)loadPluginAtURL:(NSURL *)URL {
  NSBundle *pluginBundle = [NSBundle bundleWithURL:URL];
  if (![pluginBundle load]) { // is false if pluginBundle == nil automatically.
    return NO;
  }
  id plugin = nil;
  @try { // Use @try-@catch in case the principle class doesn't respond to +alloc or -init. Otherwise the host application would crash and that kinda sucks.
    plugin = [[pluginBundle principalClass] alloc] init]; // Set the principle class in the bundle's info plist.
  } @catch (id e) {
    [bundle unload];
    return NO;
  }
  if (plugin) {
    [self.loadedPlugins addObject:plugin];// Define this as an NSMutableSet object.
    return YES;
  }
  [pluginBundle unload];
  return NO;
}

您最终可以提供一个插件可以使用的框架,其中可以包括协议和类。例如,您可以检查包的主要类是否是框架中特定类的子类,这样您就不会发送插件不响应的任何消息。

You can create a bundle. Xcode has templates for this (it is called "Loadable Bundle" and the icon is a Lego brick). You typically load a bundle using NSBundle's load method.

An example of loading it would be:

- (BOOL)loadPluginAtURL:(NSURL *)URL {
  NSBundle *pluginBundle = [NSBundle bundleWithURL:URL];
  if (![pluginBundle load]) { // is false if pluginBundle == nil automatically.
    return NO;
  }
  id plugin = nil;
  @try { // Use @try-@catch in case the principle class doesn't respond to +alloc or -init. Otherwise the host application would crash and that kinda sucks.
    plugin = [[pluginBundle principalClass] alloc] init]; // Set the principle class in the bundle's info plist.
  } @catch (id e) {
    [bundle unload];
    return NO;
  }
  if (plugin) {
    [self.loadedPlugins addObject:plugin];// Define this as an NSMutableSet object.
    return YES;
  }
  [pluginBundle unload];
  return NO;
}

You can eventually provide a framework that the plugins can use, which can include protocols and classes. You may, for example check if the principle class of the bundle is a subclass of a specific class in your framework, so you don't send any messages the plugin doesn't respond to.

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