gpt4 book ai didi

xcode - 弧: Does New Document Need to Have a Strong Pointer to It?

转载 作者:行者123 更新时间:2023-12-03 16:32:13 25 4
gpt4 key购买 nike

我刚刚使用 Xcode 工具将我的应用程序从垃圾收集移植到 ARC。当我的应用程序创建一个新窗口时,该窗口立即消失。在 GC 下,窗口仍然可见。

据我所知,在 ARC 中,任何没有强指针的对象都会消失。我有一个从 NSDocument 子类对象到属于它的窗口的强指针,但 NSWindow 在创建后立即消失。

我需要有一个指向新 NSDocument 子类对象的强指针吗?如果是这样,该指针属于什么?

- (IBAction)importLegacyDocument:(id)sender{
myDocument* theDocument = [[myDocument alloc]init];

NSWindowController* theWindowController;
theWindowController =[[NSWindowController alloc]
initWithWindowNibName:@"myDocument" owner: theDocument];
[theDocument makeWindowControllers];
[theDocument showWindows];
//WINDOW VANISHES IMMEDIATELY AFTER IT HAS BEEN CREATED
}

提前非常感谢大家提供的任何信息!

最佳答案

是的,您应该有该文档的引用。如果没有保留,您在方法内创建的任何对象都将被销毁。该代码中的 NSWindowController 实例也是如此。

@property (strong, nonatomic) myDocument *theDocument;
@property (strong, nonatomic) NSWindowController *theWindowController

(您的 AppDelegate 将是声明这些属性的好地方)

然后将创建的实例分配给您的属性:

self.theDocument = [[myDocument alloc] init];
self.theWindowController = [[NSWindowController alloc] initWithWindowNibName:@"myDocument" owner:self.theDocument];

顺便说一句 - Objective-C 约定是用大写字母命名类,因此 myDocument 应该是 MyDocument

希望这有帮助!

关于xcode - 弧: Does New Document Need to Have a Strong Pointer to It?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17508661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com