【问题标题】:Releasing "referential" variables in method scope在方法范围内释放“参考”变量
【发布时间】:2010-02-03 05:31:39
【问题描述】:

在objective-c (cocoa touch) 中,我有一系列UIViewController 可以在它们之间切换。

- (void)switchViews:(id)sender
{
    UIButton *button = (UIButton *)sender;
    UIViewController *nextViewController;
    int tag = button.tag;

    switch (tag)
    {
        // -- has already been created
        case kFinancialButton:
            nextViewController = financials;
            break;

        case kSocialButton:
            if (social == nil)
            {
                SocialViewController *socialViewController = [[SocialViewController alloc] initWithNibName:@"SocialViewController" bundle:nil];
                social = socialViewController;
                [socialViewController release];
            }
            nextViewController = social;
            break;

        case kTicketingButton:
            if (ticketing == nil)
            { 
                TicketingViewController *ticketingViewController = [[TicketingViewController alloc] initWithNibName:@"TicketingViewController" bundle:nil];
                ticketing = ticketingViewController;
                [ticketingViewController release];
            }
            nextViewController = ticketing;
            break;
    }

        ///////
------> // -- [button/nextViewController release]????
        ///////

    [self setActiveButton:button];
}

如您所见,我将其中一个视图控制器分配给“nextViewController”。我想知道的是我是否需要释放这个“本地”变量,或者是否可以不理会,因为它只是指向我的一个视图控制器(我在 dealloc 中释放)。我认为不需要发布“标签”,因为它是“原始”,对吗?按钮呢?我不太明白应该和不应该明确发布什么,所以也许我过于谨慎了。提前致谢。

【问题讨论】:

    标签: objective-c iphone cocoa-touch xcode


    【解决方案1】:

    一般来说,您只需 release 一个您已经 retain'd init'd 或 copy'd 的变量。

    编辑:

    在阅读您的代码多一点之后,您似乎还有其他与错误值有关的问题。下面的代码对我来说更有意义。这假设财务、社交和票务都是@synthesized ivars。

    - (void)switchViews:(id)sender
    {
        UIButton *button = (UIButton *)sender;
        UIViewController *nextViewController;
        int tag = button.tag;
    
        switch (tag)
        {
            // -- has already been created
            case kFinancialButton:
                nextViewController = self.financials;
                break;
    
            case kSocialButton:
                if (!social) {
                    self.social = [[[SocialViewController alloc] initWithNibName:@"SocialViewController" bundle:nil] autorelease];
                }
                nextViewController = self.social;
                break;
    
            case kTicketingButton:
                if (!ticketing) {
                    self.ticketing = [[[TicketingViewController alloc] initWithNibName:@"TicketingViewController" bundle:nil] autorelease];
                }
                nextViewController = self.ticketing;
                break;
        }
    
        // Do something with nextViewController I'd assume
    
        [self setActiveButton:button];
    }
    

    【讨论】:

    • 是的,它最终看起来像这样:social = [[SocialViewController alloc] initWithNibName:@"SocialViewController" bundle:nil];
    • 我认为我不需要“自我”。我呢?完全假设范围默认为“self”。这里。另外,我没有使用自动释放,因为我在此方法中显式保留和释放并释放。这个可以吗?谢谢,布莱恩!
    • 嗯,我用self的原因。是@synthesized setter 可以为您处理内存管理,假设您将它们设置为保留。使用它们也使其符合 KVO,尽管这在 iphone 上可能不是一个大问题,因为它在某些桌面开发中。
    • 除了上述理由之外,我还阅读了一些关于@synthesized getter 和 setter 的潜在未来优化的讨论,并且建议养成使用它们的习惯。当然,这取决于假设,所以要持保留态度。
    • 我正在合成那些视图控制器 FWIW。提及他们时不使用“自我”是错误的吗?
    猜你喜欢
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 2015-02-23
    • 1970-01-01
    • 2015-08-21
    相关资源
    最近更新 更多