Conversion to Automatic Reference Counting (ARC):

2019-03-30 03:37发布

问题:

In one of the very big projects I used auto-synthesized properties everywhere:

//MyClass.h file:
@interface MyClass : NSObject

@property (nonatomic, retain) NSString *deviceName;
@property (nonatomic, retain) NSString *deviceID;

@end

//MyClass.m file:
#import "MyClass.h"

@implementation ApplicationStatus
// no @synthesize used at all.

-(void)dealloc{

    [_deviceName release]; // gives errors only while converting to ARC with LLVM 5.0
    [_deviceID release];

    [super dealloc];
}

@end

The code above compiles well in non-ARC mode and also in older Xcode versions during ARC conversion process. When trying to convert in using newest LLVM 5.0 compiler (newest Xcode) it gives me millions of errors:

What is the cause of this? Do I have to manually create hundreds of instance variables and @synthesize them manually now? Wouldn't that be step back from 'write less code' philosophy Apple advertised on all WWDCs?

回答1:

I just hit the same problem.

Following Apple's guidance, I religiously use self. outside init and _ in init. This breaks as you wrote, when migrating to ARC in Xcode 5.

I found the easiest way is to:

@synthesise deviceName = _deviceName;

Changing all the references is just dumb, a pain, and wrong, and for read only variables, not even an option.

The autocomplete is pretty smart in setting up the synthesise statements, and you only need them for the stuff you're going to access in init.