In Swift, I notice there is no @autoreleasepool{} construct, although Swift does use ARC. What is the proper way to manage an autoreleasepool in Swift, or has it been removed for some reason?

推荐答案

The syntax is as follows:

autoreleasepool {
  /* code */ 
}

Unfortunately Apple's WWDC 2014 videos no-longer seem to be available. Incase it comes back, it was covered in WWDC 2014 session video number 418 "Improving Your App with Instruments".

The swift documentation doesn't contain anything useful currently. But you can find plenty of good information under the Obj-C Runtime Reference for NSAutoreleasePool and the Advanced Memory Management Programming Guide.

Note: Auto Release Pools are no-longer as relevant today as they were in the past. Modern code should use Automatic Reference Counting which does not use release pools at all. However there's still a lot of legacy code, including in Apple's frameworks, that use auto release pools as of 2021. If you're doing any kind of batch process on images as one example, you probably should be using an autoreleasepool block.

Swift相关问答推荐

日期格式yyyyyy—MM—dd hh:mm:ss +0000到dd MMM,以swift格式表示""""

如何将泛型函数存储到变量中?

.onReceive NSWindow.CloseNotify是否会为App中的每个窗口调用?

如何在init()中使用setter/getters?

Swift-Can无法在AudioKit中找出简单的麦克风效果-文件链

如何在AudioKit中实现自定义音效 node ?

以编程方式使UIView居中,而不影响UIKit中其他UI类的位置

对实例方法appendInterpolation的调用没有完全匹配

VisionOS TabBar预览中是否存在漏洞?

如何在SwiftUI中更改NSTextView中的 Select colored颜色 ?

暂停我的 AR 会话并清除变量和锚点的功能

数组中某些元素的总和

ConfirmationDialog取消swiftui中的错误

如何在 Swift 中使用子定义覆盖父类扩展

为什么 Apple Scrumdinger Sample App 使用两个事实来源?

自定义选取器的出现导致其他视图重新排列

如何在 SwiftUI 中正确实现Collection 夹?

如何自己快速编写 Hashable 协议

Swift 2 或 3 中的 Google Analytics 问题

修复 Swift 3 中的警告C-style for Statement is deprecated