我有一个名为MyWatchView的SwiftUI视图,带有以下堆栈:
VStack (alignment: .center)
{
HStack
{
Toggle(isOn: $play)
{
Text("")
}
.padding(.trailing, 30.0)
.hueRotation(Angle.degrees(45))
if play
{
MyWatchView.self.playSound()
}
}
}
它还具有@State private var play = false
和一个函数playSound
,如下所示:
static private func playSound()
{
WKInterfaceDevice.current().play(.failure)
}
我收到一个Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols
的错误,我认为这可能是我在Swift中的结构工作方式中所不了解的。
答案 0 :(得分:2)
您的MyWatchView.self.playSound()函数未返回View,因此您不能在HStack中使用它。
没有看到完整的代码,我只能假设您想做什么,但是这是我的猜测:如果状态变量play为true,则您想执行func playSound()?
您可以执行以下操作:
@State private var play = false {
willSet {
if newValue {
WKInterfaceDevice.current().play(.failure)
}
}
}
每当State变量play变为true时,它将执行您的静态函数。