SwiftUI-切换功能的使用-控制台日志:“无效模式'kCFRunLoopCommonModes'"-didSet不起作用 [英] SwiftUI - usage of toggles - console logs: “invalid mode 'kCFRunLoopCommonModes'” - didSet does not work

查看:609
本文介绍了SwiftUI-切换功能的使用-控制台日志:“无效模式'kCFRunLoopCommonModes'"-didSet不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SwiftUI上使用切换按钮时遇到了一个普遍的问题. 每当我使用它们时,都会出现此控制台错误:

I have a general problem using toggles with SwiftUI. Whenever I use them I get this console error:

提供给CFRunLoopRunSpecific的无效模式'kCFRunLoopCommonModes'-在_CFRunLoopError_RunCalledWithInvalidMode上中断以进行调试.每次执行该消息只会出现一次.

invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific - break on _CFRunLoopError_RunCalledWithInvalidMode to debug. This message will only appear once per execution.

此外,当我在模拟器中按下切换开关时,didSet不会打印任何内容. 有谁有主意,还是SwiftUI的错误?

In addition to this didSet does not print anything when I hit the toggle in the simulator. Does anyone have an idea, or is it a SwiftUI bug?

关于StackOverflow的其他相关问题已有一个月的历史了,似乎没有找到解决方案.

Other related questions on StackOverflow which are some month old didn't seem to find a solution.

import SwiftUI


struct ContentView: View {

    @State private var notifyCheck = false {
        didSet {
            print("Toggle pushed!")
        }
    }

    var body: some View {
            Toggle(isOn: $notifyCheck) {
                Text("Activate?")
            }
    }
}

如果这是一个错误,我想知道切换的解决方法是什么. 并不是我会成为iOS中使用切换的第一人. ;-)

If this is a bug, I wonder what the workaround for toggles is. It's not as if I would be the first person using toggles in iOS. ;-)

推荐答案

  1. 忽略该警告,它是SwiftUI的内部原理,不会影响任何内容.如果您想向Apple提交反馈.

  1. Ignore that warning, it's SwiftUI internals and does not affect anything. If you'd like submit feedback to Apple.

didSet不起作用,因为此处的self(作为View结构)是不可变的,并且@State只是属性包装器,它通过非可变设置器将包装的值存储在self之外.

didSet does not work, because self here (as View struct) is immutable, and @State is just property wrapper which via non-mutating setter stores wrapped value outside of self.

更新:在切换时执行一些操作

Update: do something on toggle

@State private var notifyCheck = false

var body: some View {

        let bindingOn = Binding<Bool> (
           get: { self.notifyCheck },
           set: { newValue in
               self.notifyCheck = newValue
               // << do anything
           }
        )
        return Toggle(isOn: bindingOn) {
            Text("Activate?")
        }
}

这篇关于SwiftUI-切换功能的使用-控制台日志:“无效模式'kCFRunLoopCommonModes'"-didSet不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆