简体   繁体   中英

refactoring to arc results in “synthesize of 'weak' property is only allowed in arc or gc mode”

i am using xcode 4.4.1 for iOS target 5.1 i've started my project using ARC, and in the middle of the project I've added some non-arc thirdparty source to my project. at this point, i've decided to try refactoring in xcode and an error occurs. it comes from one of my original source code.

i have a property of (nonatomic, weak) and i've synthesized it ==> synthesize of 'weak' property is only allowed in ARC or GC mode.

I am puzzeled - i am using arc and it complains that it is only allowed in arc??

At the moment, i've refactored the thirdparty app from other project and copied it for my project to work, but the above is a question I'd still like to understand why . Thanks! (by the way, when i do convert to arc, it says that the target currently uses ARC)

It sounds like the issue is you've already set it up as an ARC project and your are for some reason trying to convert it again. I can only assume that the process doesn't expect therefore weak references at this stage. If you set them to Assign then convert to ARC you will see that it then suggests you change them to weak.

I had the same symptom using Xcode 4.6.3 targeting iOS 6.1. I brought in a non-ARC class from another project and converted it via the Refactor menu only to have subsequent builds fail on a pre-existing class with the error you saw (“synthesize of 'weak' property is only allowed in arc or gc mode”).

The project properties clearly said it was still an ARC project but the compiler seemed to have forgotten this was the case for the class in question.

I got around the issue by setting the property to 'strong' and converting that class to ARC via the Refactor menu. Unsurprisingly it told me no changes were necessary but did offer a Save button which I clicked. I was then able to change the property back to 'weak' and compile the project. (I'm presuming some metadata got out of synch somewhere along the way.)

EDIT: I eventually had to 'Refactor' other ARC-compliant classes to ARC as although they compiled they generated warnings and caused crashes in my app. I think the moral of the story is that including your whole project in the refactoring might be safer...

instead use:

@property(nonatomic,assign)

*note - This is not the same as weak, but very similar. Weak also sets the object to nil, assign does not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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