Ekaterina Ekaterina - 1 year ago 95
Objective-C Question

ARC forbids explicit message send of 'retain'

I'm trying to use Amazon API for iOS that handles the login for Amazon website in Objective-C. I'm using this source.
However, when I implement

, I get the following error message:

ARC forbids explicit message send of 'retain'.

I've never used Objective-C before so I would appreciate if anyone could help me with the code.

Here is my code:

#import <LoginWithAmazon/LoginWithAmazon.h>
#import "AMZNAuthorizeUserDelegate.h"
#import "AMZNGetProfileDelegate.h"

@implementation AMZNAuthorizeUserDelegate

- (id)initWithParentController:(ViewController*)aViewController {
if(self = [super init]) {
parentViewController = [aViewController retain];

return self;

- (void)requestDidSucceed:(APIResult *)apiResult {
AMZNGetProfileDelegate* delegate = [[[AMZNGetProfileDelegate alloc] initWithParentController:parentViewController] autorelease];
[AIMobileLib getProfile:delegate];

- (void)requestDidFail:(APIError *)errorResponse {
NSString *message = errorResponse.error.message;
// Your code when the authorization fails.

[[[[UIAlertView alloc] initWithTitle:@"" message:[NSString
stringWithFormat:@"User authorization failed with message: %@",
errorResponse.error.message] delegate:nil
cancelButtonTitle:@"OK"otherButtonTitles:nil] autorelease] show];


Answer Source

The error message is correct: the call to retain is no longer valid.

Automatic Reference Counting, or ARC, was added to the Objective-C language in 2011 with iOS 5 and Mac OS X 10.7. Prior to ARC, you had to manage the memory usage of your app manually with calls to methods such as -retain, -release, and -autorelease. ARC manages these calls automatically at compile time, and, as such, does not allow you to call them yourself.

As @Paulw11 mentions in his comment, you should be able to replace that line with

parentViewController = aViewController

and ARC will do the correct thing automatically.