我有一个 SwiftUI 视图,它根据状态换出某些控件。我正在尝试使用 MVVM,所以我的大部分/所有逻辑都被推到了视图模型中。我发现在执行修改@Published var视图模型上的a 的复杂操作时,View不会动画。
这是一个示例,其中视图模型中的 1.0 秒计时器模拟在更改@Published var值之前正在完成的其他工作:
struct ContentView: View {
@State var showCircle = true
@ObservedObject var viewModel = ViewModel()
var body: some View {
VStack {
VStack {
if showCircle {
Circle().frame(width: 100, height: 100)
}
Button(action: {
withAnimation {
self.showCircle.toggle()
}
}) {
Text("With State Variable")
}
}
VStack {
if viewModel.showCircle {
Circle().frame(width: 100, height: 100)
}
Button(action: {
withAnimation {
self.viewModel.toggle()
}
}) {
Text("With ViewModel …Run Code Online (Sandbox Code Playgroud) 我今天升级到最新的 Xcode 12.5 Beta 2,现在我的所有 URLSession.dataTask 请求都失败并超时。我创建了一个示例项目,它提出了一个简单的请求,但每次都失败了。它适用于 Xcode 12.5 Beta 1。
这是一个简单的请求:
guard let url = URL(string: "https://hacker-news.firebaseio.com/v0/item/8863.json?print=pretty") else { fatalError() }
let startTime = Date()
let task = URLSession.shared.dataTask(with: url) { data, response, error in
let requestTime = Date().timeIntervalSince(startTime)
print("Time for request: \(requestTime)")
if let error = error {
updateLabel("requestTime: \(requestTime)\nError: \(error.localizedDescription)")
return
}
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
updateLabel("requestTime: \(requestTime)\n\(response.debugDescription)")
return
}
if let mimeType = httpResponse.mimeType, mimeType == "text/html",
let data = …Run Code Online (Sandbox Code Playgroud) 我们的应用程序允许通过SSO登录,这是通过将WKWebKit视图触发到与服务器通信的特定URL进行的,并最终重定向到我们期望的URL。在此过程中,我们获得了一个cookie,需要将其转移到SessionManager,但是,当尝试从WKHTTPCookieStore获取cookie时,我们并不总是获得回调。这是一些代码:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
let httpCookieStore = WKWebsiteDataStore.default().httpCookieStore
httpCookieStore.getAllCookies { (cookies) in
// This block not always called!!!
}
}
Run Code Online (Sandbox Code Playgroud)
这通常是在设备上初次安装该应用程序时发生的,几乎可以在设备上重现,但不能在模拟器中重现。
在这一点上,我已经尝试了所有可以想到的方法,但是我不知道为什么有时调用回调,但并非总是如此。
我正在使用 SwiftUI 创建一个新的 watchOS 应用程序并尝试使用 MVVM 架构,但是当我的 viewModel 更改时,我似乎无法在我的视图中更新文本视图。
我正在使用 watchOS 6、SwiftUI 和结合。当我认为应该使用 @ObservedObject 和 @Published 时,我正在使用它们,但更改没有像我期望的那样反映出来。
// Simple ContentView that will push the next view on the navigation stack
struct ContentView: View {
var body: some View {
NavigationLink(destination: NewView()) {
Text("Click Here")
}
}
}
struct NewView: View {
@ObservedObject var viewModel: ViewModel
init() {
viewModel = ViewModel()
}
var body: some View {
// This value never updates
Text(viewModel.str)
}
}
class ViewModel: NSObject, ObservableObject {
@Published …Run Code Online (Sandbox Code Playgroud)