@StateObject和@ObservedObject的区别
@ObservedObject创建的对象,会在View重绘的时候重新创建。@StateObject创建的对象不会,只会创建一次,所以在视图重建的时候不会受到影响。 比如 @ObservedObject var downloader = Downloader() // ... @StateObject var downloader = Downloader() VStack { ProgressView(value: downloader.progress.fractionCompleted) .progressViewStyle(LinearProgressViewStyle()) Text("\(Int(downloader.progress.fractionCompleted * 100))%") Button(action: { downloader.cancelDownload() isDownloading = false }) { Text("Cancel") }…