How to enable arc in the cocos2d Project

Source: Internet
Author: User

The basic idea is that arc is not supported.CodeSeparate from the support for arc, and set the compilation options in xcode to allow code that supports and does not support arc to coexist.

Cocos2d is the most popular 2D game engine in iOS app development. However, during cocos2d development, Apple has not launched the automatic reference counting (ARC), a mechanism to assist in memory management, the compiler automatically adds memory management statements to the code to prevent memory leaks and crashes caused by developers' negligence ). so if you use the cocos2d template to generate a project, arc is disabled. if you enable arc directly, many compilation errors will occur.

If you want to enjoy the benefits of arc, you have to find a solution on your own.

The basic idea is to use arc instead of cocos2d files. In xcode, some source files can be specified using arc.

Method from this post http://www.tinytimgames.com/2011/07/22/cocos2d-and-arc/

The method described in this article is to compile cocos2d as a separate project (without arc), reference the library file to the Enable ARC Project, and modify it a little.

However, there is a simpler method in the response (This reply is also really rude, and you are interested in looking at the response from horseshoe7). The actual steps are as follows:

Note:

-If you use a version later than cocos2d V2.0, you are lucky. Errors will not occur in versions, or. You can skip this step.

-If you use cocos2d V1.1, you will only encounter problems 4, 5, 6, and 7, which can be skipped.

-If you use 1.0.1 like me, you must modify all of the following.

1. Use llvm: Set the default compiler in build setting, search for "llvm", and select Apple llvm compiler 3.1

2. Enable arc: Search for auto and set Object C automatic reference counting to yes.

3. complie source build phase lists all. M file, check the files in cocos2d, press enter, enter "-fno-objc-arc", and press Enter. in this way, these files will not be checked by arc. we recommend that you use the appdelegate generated by cocos2d. m and rootviewcontroller. M is off arc. only use arc in your own code.

4. then compile a file and a lot of errors related to ccarray will be generated. These are from the header files of the ccarray struct. Many inline functions are used, and the llvm compiler does not recognize them. so we need to change these literary functions to common functions, and they cannot be placed in the header file. Instead, we need to add a cccarray. M file. the code is in cccarray. h, move the code yourself? Too much trouble. you don't need to change it yourself. the answer is in cocos2d V2.0, because V2 fully supports arc, so cocos2d can help you change it. download a set of V2 code and set cccarray. H and. M these two files are copied and overwritten. h file, set. add the M file to your project and compile it again. warning disappears, but there are several Link errors. it doesn't matter because cocos2d v2 has changed the names of two functions. to ccarray. in M, locate insertionsort and mergesortl, add CC _ in front, compile it, and compile ccarray.

5. ccmacro. h used merge, the three macro definitions of arc Macros in V1.1. It is useful in cccarry. M.

6. In ccdirectorios. H, remove the NSAID utoreleasepool in the ccdirectorfast function. Add the following sentence to the corresponding ccdirectorios. M file:

Static nfutoreleasepool * autoreleasepool = nil;
In fact, the variable is changed to static.

7. The structure thashelement in ccactionmanager. H will report an error. Replace it

Typedef struct _ hashelement
{
Struct ccarray * actions;
_ Unsafe_unretained idtarget;
Nsuintegeractionindex;
_ Unsafe_unretained ccaction * currentaction;
Boolcurrentactionsalvaged;
Boolpaused;
Ut_hash_handlehh;
} Thashelement;

8. at this point, only errors in your code are left: xcode has tools to switch, select Edit/refactor/convert to object C arc from the menu, select the file to be converted, and convert it. xcode will let you take a look at the places that have been deleted. You can take a look at them. Basically, dealloc, retain, and release are removed. because arc will automatically generate for you. okay, so that the compilation will be completed later and there will be no error.

Personal originality. Please indicate the source for reprinting.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.