我是一名经验丰富的 C/C++ 程序员,开始学习 Objective-C 开发。我目前正在查看 UICatalog 示例,并遇到了另一个我见过几个地方但从未理解过的成语实例。
代码:
ButtonsViewController *buttonsViewController = [[ButtonsViewController alloc] initWithNibName:@"ButtonsViewController" bundle:nil];
[self.menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
NSLocalizedString(@"ButtonsTitle", @""), kTitleKey,
NSLocalizedString(@"ButtonsExplain", @""), kExplainKey,
buttonsViewController, kViewControllerKey, nil]];
[buttonsViewController release];
AFAIK,这分配并初始化一个 ButtonsViewController,为 ButtonsViewController 创建一个 NSDictionary,并将字典添加到一个名为 menuList 的 NSMutableArray(这是上面代码所在的 MainViewController 的成员变量),然后释放它刚刚创建的 buttonViewController。稍后,MainViewController 在必要时使用字典条目将视图切换到buttonsViewController。
我的问题是:为什么buttonViewController 在这段代码之后仍然有效?它被分配和释放,之间没有“保留”。向 NSDictionary 或 NSMutableArray 添加内容是否具有隐式“保留”?如果是这样,我应该能够以某种方式弄清楚这一点,还是只是你必须阅读和记住的那些事情之一?