Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios SwiftUI:如何将Int转换为绑定<;Int>;?_Ios_Swift_Enums_Swiftui_Binding - Fatal编程技术网

Ios SwiftUI:如何将Int转换为绑定<;Int>;?

Ios SwiftUI:如何将Int转换为绑定<;Int>;?,ios,swift,enums,swiftui,binding,Ios,Swift,Enums,Swiftui,Binding,我需要将Int(索引)转换为绑定,以便我的UI在SwiftUI中进行反应性更新,我该如何做 enum CurrentTodayView: String, CaseIterable, Identifiable { var id: String { self.rawValue } case Recovery = "Recovery" case Exertion = "Exertion" case Sleep = "Sl

我需要将Int(索引)转换为绑定,以便我的UI在SwiftUI中进行反应性更新,我该如何做

enum CurrentTodayView: String, CaseIterable, Identifiable {
    var id: String { self.rawValue }
    case Recovery = "Recovery"
    case Exertion = "Exertion"
    case Sleep = "Sleep"
    case Energy = "Energy"
    
    static func index(of aStatus: CurrentTodayView) -> Binding<Int> {
        let elements = [CurrentTodayView.Recovery, CurrentTodayView.Exertion, CurrentTodayView.Sleep, CurrentTodayView.Energy]
   
        //This returns an Int but I need to return a Binding<Int>
        return elements.firstIndex(of: aStatus)!
     }
}
enum CurrentTodayView:字符串,可大小写,可识别{
变量id:字符串{self.rawValue}
案例恢复=“恢复”
case employment=“employment”
case Sleep=“Sleep”
案例能量=“能量”
静态func索引(aStatus:CurrentTodayView的)->绑定{
let elements=[CurrentTodayView.Recovery,CurrentTodayView.Employment,CurrentTodayView.Sleep,CurrentTodayView.Energy]
//这将返回一个Int,但我需要返回一个绑定
返回元素.firstIndex(of:aStatus)!
}
}

-始终可以通过闭包手动创建绑定。如果无法更新该值,则可以使用该值创建它们,因为不需要执行更新操作。@GarySabo能否在
视图中显示如何使用该值?您不能在这样的枚举中存储可变状态,因此您很可能会在视图中查找类似于
@state
的内容,或者在
可观察对象上查找类似于
@Published
的内容,这两者都是
绑定
,而不需要做任何额外的操作。