在 Objective-C 中封装 C 库的技巧

发布于 2024-08-02 05:41:53 字数 326 浏览 2 评论 0 原文

我有一个用 C 编写的库,我想在 Mac 或 iPhone 上的 Objective-C 应用程序中使用它。

不幸的是,由于该库是由开源领域的个人编写的,因此文档非常稀疏且不完整。虽然我可以弄清楚如何使用库中的内容,但我并没有真正了解整个代码库。

我想做的是将库包装成 Objective-C 中一些易于使用和可转移的类。

  • 有人对如何解决这个问题有任何建议吗?
  • 关于获得图书馆结构的视觉层次结构的最佳方法有什么建议吗?
  • 我将如何决定如何最好地构造包装器以实现可重用性和易用性?

任何和所有的帮助将不胜感激,谢谢!

I have a library written in C that I would like to use in an Objective-C app, either on the Mac or the iPhone.

Unfortunately, since this library is being written by individuals in the open source space, the documentation is quite sparse and incomplete. While I can figure out how to use the stuff in the library, I don't really have an overview of the entire code base.

What I would like to do is wrap the library up into some easily usable and transferrable classes in Objective-C.

  • Does anyone have any tips on how to approach this?
  • Any advice on the best way to get a visual hierarchy of how the library is structured?
  • How would I go about deciding how to best structure the wrapper for reusability and ease of use?

Any and all help will be greatly appreciated, thanks!

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

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

发布评论

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

评论(3

渔村楼浪 2024-08-09 05:41:53

我自己也做过几次这样的事。这可能很有趣——这是您修复(或至少隐藏)错误代码的机会!

您可以使用 Doxygen 来获取代码的可视化层次结构(尽管我只将其用于 C++ 库) ,它也可以与 C) 或任何其他免费工具一起使用。

如果库的设计或文档不完善,则不要像底层库一样构建包装类。这是您考虑用户的观点以及他们将如何使用代码的机会。首先编写测试用例来弄清楚这一点,和/或与一些已经使用该库的人交谈。

与您正在做的事情相匹配的两个很好的设计模式是 Adapter外观

I've done this a few times myself. This can be fun -- it's your chance to fix (or at least hide) bad code!

You can use Doxygen to get a visual hierarchy of the code (although I've only used it for C++ libraries, it also works with C), or any of the other free tools out there.

Don't structure your wrapper class like the underlying library if the library isn't designed or documented well. This is your chance to consider the point of view of the user and how they are going to be using the code. Write your test cases first to figure that out, and/or talk to some people who use the library already.

Two nice design patterns that match up with what you're doing are Adapter and Facade.

一桥轻雨一伞开 2024-08-09 05:41:53

首先,请记住:C 库 Objective-C 库。实际上,您根本不需要进行任何包装,尽管如果库接口特别麻烦,您可能需要这样做。

其次,如果您决定编写一个库包装器,请保持简单。确定您实际计划使用的库的核心函数,并考虑如何最好地为这些函数提供接口,并且只为这些函数提供接口,同时考虑到您的预期用途。设计一个您想要使用的接口,然后通过库实现它。

First, remember: a C library is an Objective-C library. You don't actually need to do any wrapping at all, although you may want to if the library interface is especially cumbersome.

Second, if you decide that you want to write a library wrapper, keep it simple. Identify the core functions of the library that you actually plan to use, and think about how best to provide an interface to those functions and those functions only, with your intended usage in mind. Design an interface that you want to work with, then implement it over the library.

薄凉少年不暖心 2024-08-09 05:41:53

自从 ARC(自动引用计数)被添加到 Apple 编译器和库中后,Objective-C 和 C 就不再可以自由互换。 (这里是 ARC 文档和教程的列表。)您需要更彻底地考虑内存分配问题,并且您可能只想“桥接”库。请参阅这个SO问题以及其中的一些链接,了解Apple如何Obj-C 和 C 库之间的桥梁。

Since ARC (Automatic Reference Counting) was added to the Apple compilers and libraries, Objective-C and C are no longer so freely interchangeable. (Here's a list of ARC documentation and tutorials.) You need to consider the memory allocation issues much more thoroughly, and you might just want to "bridge" the libraries. See this SO question and some of the links from there, about how Apple bridges between Obj-C and C libraries.

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