DevPrep

All topics › Objective-C

MRC vs ARC — what's the difference?

Middle PRO

Essence

Both models are reference counting: an object has a retain count, and when it reaches zero dealloc runs. The difference is who inserts the retain/release/autorelease calls.

#import <Foundation/Foundation.h>

@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
@end

#if !__has_feature(objc_arc)
// MRC: the setter is written by hand
@implementation Person
- (void)setName:(NSString *)name {
    if (_name != name) {
        [_name release];
        _name = [name copy];
    }
}
- (void)dealloc {
    [_name release];
    [super dealloc];
}
@end
#else
// ARC: the compiler generates the same thing from the copy attribute
@implementation Person
@end
#endif

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store