How can one turn ARC off for specific file

2020-07-03 07:34发布

问题:

I know I am supposed to add the compiler flag -fno-objc-arc to the compile sources in XCode 4 to accomplish this. But it isn't working. Even with the added flag I am still getting errors in my KeychainItemWrapper.m file claiming I need to use __bridge for C pointers.

My project is ARC safe, but the Apple provided reference for interacting with the Keychain is not. I would like to just disable ARC for that single file. What else am I missing here?

回答1:

I've had this happen more than once for me in 4.2 in different projects, but then I've not been able to reproduce it reliably enough for a bug report. But I can say in my cases, a clean build cleared it up.



回答2:

Click the Edit menu, go to Refactor > Convert to Objective-C ARC... Click the disclosure triangle next to your project name and check all files for which you want to use ARC.



回答3:

It is possible to disable ARC for individual files by adding the -fno-objc-arc compiler flag for those files.

You add compiler flags in Targets -> Build Phases -> Compile Sources. You have to double click on the right column of the row under Compiler Flags. You can also add it to multiple files by holding the cmd button to select the files and then pressing enter to bring up the flag edit box.

I created a sample project that has an example: https://github.com/jaminguy/NoArc

See this answer for more info: Disable Automatic Reference Counting for Some Files