狂人山庄 Madman Hills
  • Home
  • About Me
  • 『罩.片』
Sign in Subscribe

APP

A collection of 1 post
SwiftUI

@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") }
2023年7月15日 1 min read
Page 1 of 1
狂人山庄 Madman Hills © 2025
Powered by Ghost