-1

I was playing with binding in playground using SwiftUI, it's a very simple code

struct Car:View {
    var kar = ""
    @Binding var startAnimation: Bool = false
    var body: some View {
        HStack {
            Spacer()
            Text(kar)
                .font(.custom("Arial", size: 100))
                .offset(x: self.startAnimation ? 0 - UIScreen.main.bounds.width + 100: 0)
        }
    }
}

But playground gave an error that says "Extraneous Argument Label ", I literally see no problem, especially when i code using Xcode. so is there a way to somehow hack it or did i miss something??

The error

ios coder
  • 1
  • 4
  • 31
  • 91
Farhandika
  • 437
  • 1
  • 3
  • 16

1 Answers1

1

You can not or you should not give direct value to Binding, it bind data, you should use State for example like in code


enter image description here


with State:

import SwiftUI

struct CarView: View {
    
    @State private var startAnimation: Bool = Bool()
    
    var body: some View {
        
        VStack {
            
            Text("")
                .font(Font.system(size: 100))
                .offset(x: startAnimation ? 50.0 - UIScreen.main.bounds.width/2 : UIScreen.main.bounds.width/2 - 50.0)
            
            Button("Drive!") { startAnimation.toggle() }
                .font(Font.headline)
                .padding()
            
        }
        .animation(.easeIn(duration: 3.0), value: startAnimation)
        
    }
}

With Binding:

import SwiftUI

struct ContentView: View {
    
    @State private var startAnimation: Bool = Bool()
    
    var body: some View {
        
        VStack {
            
            CarView(startAnimation: $startAnimation)
            
            Button("Drive!") {
                
                startAnimation.toggle()
            }
            .font(Font.headline)
            .padding()
            
        }
        .animation(.easeIn(duration: 3.0), value: startAnimation)
        
    }
}

struct CarView: View {

    @Binding var startAnimation: Bool
    
    var body: some View {
        
        Text("")
            .font(Font.system(size: 100))
            .offset(x: startAnimation ? 50.0 - UIScreen.main.bounds.width/2 : UIScreen.main.bounds.width/2 - 50.0)

    }
}
ios coder
  • 1
  • 4
  • 31
  • 91