好的,Apple给我们带来了ARC,这很棒.在将我的应用程序重构为ARC之后,几乎所有工作都正常,现在开发和维护起来要容易得多.
只有一个问题我仍然无法弄清楚.
我的工作管理程序在自己的窗口中显示提案,订单等的不同详细信息.所以我有一个特殊的类,其中WindowControllers被分配并使用initWithWindowNibName启动,然后窗口显示为showWindow:
DetailWindowController *proposalWindowController = [[DetailWindowController alloc] initWithWindowNibName:@"ThePorposalWindow"];
[proposalWindowController showWindow:nil];
Run Code Online (Sandbox Code Playgroud)
在ARC之前,WindowController的实例执行了如文档中所示的发布:
- (void)windowWillClose:(NSNotification *)notification
{
[self autorelease];
}
Run Code Online (Sandbox Code Playgroud)
但是现在使用ARC这是不可能的,更糟糕的是,在我分配和启动WindowController的特殊类中,ARC释放了相同的windowController,因为没有指向windowController的指针.
我的想法是将windowController复制到一个可变数组中:
[proposalWindowArray addObject:proposalWindowController];
[[proposalWindowArray lastObject] showWindow:nil];
Run Code Online (Sandbox Code Playgroud)
并在windowControllers委托方法windowWillClose中我向我的特殊类发布通知:
- (void)windowWillClose:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"ProposalWindowWillClose" object:[[self window] windowController] userInfo:nil];
}
Run Code Online (Sandbox Code Playgroud)
在我的特殊类中,我听取通知并从数组中删除对象:
- (void) proposalWindowWasClosed: (NSNotification *) notification
{
[proposalWindowArray removeObjectIdenticalTo:[notification object]];
}
Run Code Online (Sandbox Code Playgroud)
它有效,但我仍然不相信这是正确的方法.
有没有人有同样的问题或提示让它变得更好?
macos cocoa design-patterns memory-management automatic-ref-counting