简体   繁体   English

关于在Objective-C中包装C库的提示

[英]Tips on wrapping a C library in 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. 我有一个用C编写的库,我想在Objective-C应用程序中使用,无论是在Mac还是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. 我想要做的是将库包装到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! 任何和所有的帮助将不胜感激,谢谢!

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. 您可以使用Doxygen来获得代码的可视层次结构(尽管我只将它用于C ++库,它也适用于C),或者其他任何免费工具。

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 . 与您正在做的事情相匹配的两个不错的设计模式是AdapterFacade

First, remember: a C library is an Objective-C library. 首先,请记住:C库一个Objective-C库。 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. 设计一个您想要使用的接口,然后通过库实现它。

Since ARC (Automatic Reference Counting) was added to the Apple compilers and libraries, Objective-C and C are no longer so freely interchangeable. 由于ARC(自动引用计数)已添加到Apple编译器和库中,因此Objective-C和C不再可以自由替换。 ( 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. 这里是ARC文档和教程的列表。)您需要更彻底地考虑内存分配问题,并且您可能只想“桥接”库。 See this SO question and some of the links from there, about how Apple bridges between Obj-C and C libraries. 请参阅此SO问题以及其中的一些链接,了解Apple如何在Obj-C和C库之间架起桥梁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM