Using ARC enabled code in your non-ARC project

来源:互联网 发布:ubuntu安装卡在logo 编辑:程序博客网 时间:2024/05/16 19:26


I’ve always been a bit late at adopting new technologies, such as this brilliant thingy called ARC from Apple. I didn’t want to touch it because I was quite confident with my Objective-C memory management knowledge. Until one day when I ran the analysis check in Xcode, I was surprised to see a long list of memory warnings coming from a 3rd party library that I was using.

It turned out that that library was using ARC, and I didn’t know it because, well, I didn’t know ARC.

So I was faced with this - either I needed to convert my whole project to using ARC, or I needed to find a non-ARC alternative to that library. I googled a lot trying to see if there are other options. There were tons of articles talking about how one can integrate non-ARC enabled code in an ARC enabled project, or how one should convert projects to using ARC, which is good advice, but I didn’t want to do that at that time since I was about to release.

And then I read this excellent article Mixing ARC and Non-ARC Code with Static Libraries written by Duane Fields on how one can mix ARC and non-ARC code together using static library. Although it still talks about the opposite way, I was inspired by Duane’s solution and decided to follow that path but turn things around.

Steps

So here is how I did it:

  1. Create a new target in the non-ARC project in Xcode. Select the “Cocoa Touch Static Library” template, and make sure “Use Automatic Reference Counting” is checked. This target will be used to hold the ARC enabled code.

    picture

    picture

  2. Now that the new target is created, you can start to add the ARC code files into this target, just as you would normally do for a normal Xcode project. Keep in mind that when Xcode asks for what files to add, there is an option named “Add to targets”. In that option the newly created target should be checked.

    picture

  3. Let’s figure out target dependencies and linker settings. Click on the name of your project in project navigator and open the project settings page. Select your main target in “Targets”, click on “Build Phases”, and expand “Target Dependencies”. Click on the “+” button and add the new static library target from there. In this way Xcode will be able to handle the relationship between the targets and build in the right order (if both targets are within the same Xcode workspace, then this is not required but recommended. Read this for a reference).

    picture

    And then while the “Build Phases” tab is still open, expand “Link Binary with Libraries”, click on the “+” button, and add your static library target from there. If there are other framework/libraries that your static library target depends on, you might want to add them as well.

    picture

    There you should have it. You can use your favorite retain/release etc in the main target, and keep the ARC “plagued” code separated in static libraries, at least for a while, until you are comfortable with ARC. ;)

A bit testing

I wasn’t sure if this approach would work at first, since there wasn’t any one talking about this approach on the internet (not that I am aware of, if there are please let me know), and therefore my best bet would be to test out myself. So I did quite some memory testing. I tested allocations and memory leaks using Instruments, and verified no instances of ARC enabled classes were leaked. I also manually added debug logs in the dealloc methods of ARC enabled classes, and verified that the class instances that were created were successfully deallocated.

Given that most of ARC (other than weak reference) is just compiler magic, as long as the correct compiler (Apple LLVM compiler 3.0 and beyond) is used, and that the minimal deployment target (iOS 4.0 and newer) is met, I assume the output will just be fine. If you run into any issues, please let me know by leaving a comment below.

Enjoy!

----------------------------------------------------------------------------------------------------------------------------------

1.引用:原文地址:http://iappexperience.com/post/23288195515/using-arc-enabled-code-in-your-non-arc-project 

2.Using DTCoreText in Non ARC Projects(https://github.com/Cocoanetics/DTCoreText/wiki/Using-DTCoreText-in-Non-ARC-Projects)


原创粉丝点击