如何在块中使用 NSTextView?

How can I use NSTextView in block?

我们知道Apple Transitioning to ARC Release Notes

Which classes don’t support weak references?

You cannot currently create weak references to instances of the following classes:

NSATSTypesetter, NSColorSpace, NSFont, NSMenuView, NSParagraphStyle, NSSimpleHorizontalTypesetter, and NSTextView.

Note: In addition, in OS X v10.7, you cannot create weak references to instances of NSFontManager, NSFontPanel, NSImage, NSTableCellView, NSViewController, NSWindow, and NSWindowController. In addition, in OS X v10.7 no classes in the AV Foundation framework support weak references.

所以问题是 How can use NSTextView in block?,如果不使用 weak ,该块将持有 strong NSTextView ref。

我担心应用无法释放 NSTextView。

我该怎么办?

试试下面的方法:

NSTextView * __block textView = [[NSTextView alloc] init…];

添加 __block 以便在块中解决这个问题,为了发布目的,您需要在完成块中或根据您的使用情况明确发布它。