我在仪器的泄漏中运行我们的应用程序,它检测到String(contentsOfFile)
泄漏。
我们的应用程序中有此类:
class CoordinatesUtil {
static let shared = CoordinatesUtil()
private init() {
for filename in ["group_one", "group_two", "group_three"] {
loadFromCsv(filename)
}
}
private func loadFromCsv(_ filename: String) -> [[String]]? {
guard let path = Bundle.main.path(forResource: filename, ofType: "csv") else {
return nil
}
do {
let content = try String(contentsOfFile: path)
let lines = content.components(separatedBy: CharacterSet.newlines)
.map({$0.components(separatedBy: ",")})
.filter({$0.count == 6})
return lines
} catch {
return nil
}
}
}
然后在我们的AppDelegate中将其初始化如下:
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Initialize CoordinatesUtil
DispatchQueue.global().async {
let _ = CoordinatesUtil.shared
}
}
}
注意:CSV文件的内容包含从500到7K坐标的行。
有人知道为什么String(contentsOfFile)
在这里泄漏吗?