问题
When I run the following code (a simplified example, created to demonstrate the crash), it executes as expected when I choose Run (both os_log messages print in Console).
However, when I open it in Instruments from the Memory Debug Navigator - by pressing Restart - it crashes (only the first os_log message prints in Console).
The crash occurs at observe(...).
import os
import UIKit
class ObserverCrashingExample: NSObject {
@objc private var animation: UIViewPropertyAnimator?
private var observer: NSKeyValueObservation?
override init() {
super.init()
animation = UIViewPropertyAnimator( duration: 1, curve: .linear, animations: { })
animation!.pauseAnimation()
os_log("X_AMPLE Executes")
observer = animation!.observe(\.isRunning, options: [.new, .old]) { _ , _ in }
os_log("X_AMPLE Does not execute")
}
required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
}
I am running Xcode 9.3, Swift 4.1, iOS 11.3 deployment target.
Any ideas? Is this a compiler issue?
Cheers
回答1:
I tried three word-arounds:
- Disabling "Record Reference Count". This stopped it from crashing. But the instruments showed me much more leaks than usual, which makes locating real leaks impossible.
- Commenting all my KVO calls. Didn't work in my case.
- Downgrading my code to Swift 4.0. I re-installed Xcode 9.2, and it works now.
来源:https://stackoverflow.com/questions/49729233/observing-property-crashes-instruments-leaks-profile-xcode-9-3-swift-4-1