什么是动态创造?

发布于 2024-11-06 06:47:57 字数 594 浏览 1 评论 0 原文

我最近读到动态创建是 Cocoa 中的设计模式之一。但是,我不太明白它是如何工作的。所以我需要你在设计中实现的澄清。

  1. 它是什么?为什么以及何时使用这种设计模式?
  2. 我读到您使用 NSClassFromString() 来访问该类。我假设当我想使用我正在处理的项目中不存在的类时我会使用它。通常当我想使用某些类时,我将它们导入到 header 中。使用此方法是否会跳过#import 过程?

    Class JavaArrayList = NSClassFromString(@"java.util.ArrayList");
    

    我引用上面的代码作为示例。如果按照上面的代码做,这意味着我可以创建一个新的JavaArrayList类并使用其中的方法,对吗?

    JavaArrayList *foo = [[JavaArrayList alloc] init];
    [foo useMethodBelongJava:doWhateverTask];
    
  3. 使用这种设计模式有什么好处?尤其是在 iPhone 开发中。

I recently read about Dynamic Creation as one of the design pattern in Cocoa. However, I don't really understand how it works. So I need clarification from you who have implemented in your design.

  1. What is it? Why and when would you use this design pattern?
  2. I have read that you use NSClassFromString() to access the class. I assume that I use this when I want to use class that doesn't exist within the project I'm working on. Usually when I want to use certain class, I imported them in header. Does using this approach skip the #import process?

    Class JavaArrayList = NSClassFromString(@"java.util.ArrayList");
    

    I quote the code above as example. If do according to the code above, that means I can create a new JavaArrayList class and use the methods in it right?

    JavaArrayList *foo = [[JavaArrayList alloc] init];
    [foo useMethodBelongJava:doWhateverTask];
    
  3. What are the benefits of using this design pattern? Especially in iPhone Development.

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

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

发布评论

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

评论(2

奈何桥上唱咆哮 2024-11-13 06:47:57

您的示例似乎正在使用该模式来实例化 Java 类。在过去(我认为大约是 MacOS 10.4),Apple 有一些称为 Cocoa-Java Bridge 的技术,它允许您在 Objective-C 代码中使用 Java 类。您必须以指定的方式实例化它们,因为它们没有要导入的 Objective-C 头文件。

但是,从 Snow Leopard 开始,Java Bridge 不再存在,因此您问题中的代码将不再起作用。

从 Objective-C 调用 Java 类的推荐解决方案现在是 JNI 。看看这个问题如果这就是你的问题正在尝试做。

  1. 这是什么?您为什么以及何时使用这种设计模式?

回到 NSClassFromString,除了实例化 Java 类(正如我提到的,它不再做任何事情!)之外,它还有其他用途。举个例子,最近我编写了一个用于解析 Web 服务响应的库。为了使其能够与不同的 Web 服务配合使用,我让它读取了一个配置文件,该文件描述了它所期望的数据格式。对于 Web 服务中的每个字段,我的配置文件指定了要实例化的 Cocoa 类。因此,在我的代码中,我有一个作为字符串的 Cocoa 类名。为了实例化我想要的对象,我使用 NSClassFromString 将其转换为 Class 对象。

  • 通常当我想使用某些类时,我将它们导入到 header 中。使用此方法是否会跳过#import 过程?
  • 它可以做到。 NSClassFromString 将实例化运行时存在的任何类,因此您不需要标头就可以使用它。如果没有标头,每当您尝试使用新实例化的类时,您都会收到一堆“可能不会响应选择器”的警告,因为编译器没有足够的信息来提供帮助。然而,在许多 NSClassFromString 有用的情况下,头文件不可用。

    Your example appears to be using that pattern to instantiate a Java class. In the old days (up to about MacOS 10.4 I think), Apple had some technology called the Cocoa-Java Bridge, which let you use Java classes within Objective-C code. You had to instantiate them in the manner specified, because they didn't have Objective-C header files to import.

    However, as of Snow Leopard, the Java Bridge no longer exists, so the code in your question won't work any more.

    The recommended solution for calling a Java class from Objective-C is now JNI. Take a look at this question if that is what you're trying to do.

    1. What is it? Why and when would you use this design pattern?

    Coming back to NSClassFromString, it has other uses besides instantiating Java classes (which, as I mentioned, it doesn't do any more!). For an example, recently I wrote a library for parsing the response from a web service. In order to make it work with different web services, I had it read in a configuration file that described the data format it was expecting. For each field in the web service, my configuration file specified which Cocoa class to instantiate. Thus, in my code, I had a Cocoa class name as a string. To instantiate the object I wanted, I used NSClassFromString to turn it into a Class object.

    1. Usually when I want to use certain class, I imported them in header. Does using this approach skip the #import process?

    It can do. NSClassFromString will instantiate any class that is present at run time, so you don't need the header to be able to use it. If you don't have the header, you'll get a bunch of warnings of "may not respond to selector" whenever you try and use your newly instantiated class, as the compiler doesn't have enough information to be helpful. However, in many circumstances where NSClassFromString is useful, the header files aren't available.

    萌化 2024-11-13 06:47:57

    请参阅此链接:
    需要有关 NSClassFromString 的建议

    iPhone 的唯一真正好处是能够引用较新 API 中的类,并且仍然针对旧的 API。从 4.0 开始,您可以通过设置项目的部署目标来完成此操作。我实在看不出你有什么理由再用它来进行 iPhone 编程。

    这仅适用于 Objective-C 类。您无法将 java 对象导入您的 iPhone 应用程序。

    See this link:
    need advise about NSClassFromString

    The only real benefit for iPhone was being able to reference classes from newer APIs and still target the old APIs. Since 4.0 you can do this anyway by setting the deployment target of your project. I can't really see any reason you would use it for iPhone programming any more.

    This would only work for objective-C classes. You can't import java objects into your iphone app.

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