如何在块中设置ivar而不创建保留周期

时间:2014-01-19 09:14:40

标签: ios objective-c objective-c-blocks ivar

我正在尝试在NSObject块中启动名为FormObject的{​​{1}}子类。 JavascriptCore应该是FormObject,直到我在nil块中设置它为止。我需要在此块中设置它,因为在我显示JavascriptCore后,我在UIActionSheet方法FormObject

中保存了此UIActionSheetDelegate

以下是代码:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

我知道问题是因为

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (actionSheet.tag == kSaveFormConfirmationActionSheetTag) {
        if ([ButtonTitle isEqualToString:NSLocalizedString(@"Yes", @"Yes")]) {
            NSLog(@"Saving form data");
            NSLog(@"%@",formData);
            NSLog(@"%@",formData.dictionary);
            if (formData) {
                [[AutoFillManager defaultManager] saveFormData:formData];
                formData = nil;
            }
        }
        else if ([ButtonTitle isEqualToString:NSLocalizedString(@"Never for this Website", @"Never for this Website")]) {
            NSString *formURLString = [formData.urlString copy];
            formData = nil;
            formData = [[FormObject alloc] initWithDisabledSite:formURLString];
            [[AutoFillManager defaultManager] saveFormData:formData];
            formData = nil;

        }
        else {
            self.formData = nil;
        }
    }
}



- (JSContext *)getCurrentJavascriptContext {
    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

    NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"JSTools" ofType:@"js"];
    NSString *scriptString = [NSString stringWithContentsOfFile:scriptPath encoding:NSUTF8StringEncoding error:nil];
    [context evaluateScript:scriptString];
    context[@"print"] = ^(NSString *string) {
        NSLog(@"%@",string);
    };

    __weak AutoFillManager *afm = [AutoFillManager defaultManager];
    __weak UIToolbar *weakToolbar = toolbar;
    __weak typeof(self) weakSelf = self;

    context[@"receiveForm"] = ^(NSString *urlString, NSString *username, NSString *usernameFieldID, NSString *usernameFieldName, NSString *password, NSString *passwordFieldID, NSString *passwordFieldName) {

        NSURL *url = [NSURL URLWithString:urlString];

        for (NSDictionary *savedForm in [afm savedForms]) {
            FormObject *form = [[FormObject alloc] initWithFormDictionary:savedForm];
            if ([[url host] isEqualToString:form.urlString] && [username isEqualToString:form.username] && form.neverForThisSite == NO) {
#ifdef DEBUG
                NSLog(@"Site already saved");
#endif
                return;
            }

            if ([[url host] isEqualToString:form.urlString] && form.neverForThisSite == YES) {
#ifdef DEBUG
                NSLog(@"Never for this site");
#endif
                return;
            }
        }

        formData = [[FormObject alloc] initWithUsername:username withUsernameID:usernameFieldID withUsernameName:usernameFieldName withPassword:password withPasswordID:passwordFieldID withPasswordName:passwordFieldName withURLString:[url host]];
        UIActionSheet *saveFormConfirmationActionSheet = [[UIActionSheet alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"Would you like to save this password?", @"Would you like to save this password?")] delegate:weakSelf cancelButtonTitle:NSLocalizedString(@"Not Now", @"Not Now") destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"Yes", @"Yes"), NSLocalizedString(@"Never for this Website", @"Never for this Website"), nil];
        saveFormConfirmationActionSheet.tag = kSaveFormConfirmationActionSheetTag;
        [saveFormConfirmationActionSheet showFromToolbar:weakToolbar];
    };

    return context;
}

如果我添加说明符formData = [[FormObject alloc] initWithUsername:username withUsernameID:usernameFieldID withUsernameName:usernameFieldName withPassword:password withPasswordID:passwordFieldID withPasswordName:passwordFieldName withURLString:[url host]]; Xcode会警告我将其更改为__weak。如果我将其更改为__block,则__block之后仍为formData

如何正确设置此块中的ivar?

解决方案是让ivar成为属性并使用弱自我来设置属性。

nil

1 个答案:

答案 0 :(得分:7)

访问块内的实例变量将使块捕获对拥有实例变量的对象的引用 - self

您可以通过将实例变量转换为属性,创建对self的弱引用以及在弱自我上设置属性来避免这种情况。

或者,您可以使用->直接访问ivar,再次使用弱自我,但我更喜欢前一种解决方案。