Ios SwiftUI:如何将Int转换为绑定<;Int>;?
我需要将Int(索引)转换为绑定,以便我的UI在SwiftUI中进行反应性更新,我该如何做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
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
的内容,这两者都是绑定
,而不需要做任何额外的操作。