ARC禁止“保留”

时间:2016-08-07 10:44:04

标签: ios objective-c amazon-web-services

我正在尝试使用适用于iOS的Amazon API来处理Objective-C中的Amazon网站登录。我正在使用this source。 但是,当我实现AMZNAuthorizeUserDelegate时,我收到以下错误消息:

  

ARC禁止发送'retain'的明确消息。

我之前从未使用过Objective-C,所以如果有人能帮我解决这些问题,我将不胜感激。

这是我的代码:

#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];
}

@end

1 个答案:

答案 0 :(得分:2)

错误消息正确:对retain的调用不再有效。

2011年,使用iOS 5和Mac OS X 10.7将

Automatic Reference Counting或ARC添加到Objective-C语言中。在ARC之前,您必须通过调用-retain-release-autorelease等方法来手动管理应用的内存使用情况。 ARC在编译时自动管理这些调用,因此,不允许您自己调用它们。

正如@ Paulw11在评论中提到的那样,你应该能够用

代替那一行
parentViewController = aViewController

并且ARC将自动执行正确的操作。