【发布时间】:2010-12-30 01:03:48
【问题描述】:
您能告诉我以下代码是否 100% 正确吗?特别是dealloc 部分
FirstViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@class SecondViewController
@interface FirstViewController : UIViewController
{
SecondViewController *SecondController;
}
- (IBAction)SwitchView;
@property (nonatomic, retain) IBOutlet SecondViewController *SecondController;
@end
FirstViewController.m
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize SecondController;
- (IBAction)SwitchView
{
SecondController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
SecondController.modalTransitionStyle = UIModalPresentationFullScreen;
[self presentModalViewController:SecondController animated:YES];
[SecondController release];
}
/// OTHER CODE HERE ///
- (void)dealloc
{
[SecondController release];
[super dealloc];
}
@end
谢谢!
【问题讨论】:
-
一个问题,谁负责解散 SecondController 实例?
-
你真的真的不想调用你的实例变量SecondController。叫它
secondController。我很惊讶编译器甚至编译了该代码。 -
@bbum 为什么编译器会检查变量大小写和/或拼写?!
-
我将
SecondController和SecondViewController读为同一件事;我的错。但也说明了为什么实例变量应始终以小写字母开头。静态分析器应该标记这个。 -
@mvds:我认为@bbum 遵循惯例,即实例变量通常为小写,以便轻松将它们与类标识符区分开来。
标签: iphone objective-c ios memory-management