0
我剛開始在Objective-C中編程。在此之前,我主要是C#人員。Iphone中的內存管理。延遲初始化
請讓我知道如果我以正確的方式釋放下面的代碼中的內存。
@implementation CalculatorViewController
-(CalculatorBrain *) brain
{
if(!brain)
{
brain = [[CalculatorBrain alloc] init];
}
return brain;
}
-(IBAction) operationPressed: (UIButton *) sender
{
NSString *operation = sender.titleLabel.text;
if(userInMiddleOfTypingDigit)
{
[self brain].operand = display.text.doubleValue;
userInMiddleOfTypingDigit = NO;
}
double result = [[self brain] performOpeaation:operation];
[display setText:[NSString stringWithFormat:@"%g",result]];
}
- (void)dealloc
{
[brain release];
[super dealloc];
}
基本上我在做斯坦福大學教程中顯示的延遲初始化。但不知道我是否正確釋放內存。
如果有問題請糾正我。
謝謝,
@Sandeep只是一個小建議:儘量避免使用方法和變量的等效名稱,因爲它很混亂。 – Max 2011-06-14 21:16:09