UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: How to make previews happy?

Forums > SwiftUI

I have this view that accepts a function as a parameter not sure what parameter to pass to the preview? Thanks for having a look The way I have it now I get this error: Cannot convert value of type '(() -> Void).Type' to expected argument type '() -> Void'

struct LikeButton: View {
    @State var isPressed = false
    @State var scale : CGFloat = 1
    @State var opacity  = 0.0
    let btnAction: () -> Void

    var body: some View {

        Button(action:{
            btnAction()
            isPressed.toggle()
            withAnimation (.linear(duration: 0.2)) {
                scale = self.scale == 1 ? 1.3 : 1
                opacity = self.opacity == 0 ? 1 : 0
            }
        }){
            ZStack {
                Image(systemName: "heart.fill")
                    .font(.title3)
                    .opacity(isPressed ? 1 : 0)
                    .scaleEffect(isPressed ? 1.0 : 0.1)
                    .animation(.linear)
                Image(systemName: "heart")
                    .font(.title3)
                    .foregroundColor(Color("BrandPrimary"))
            }.font(.system(size: 40))
        }
        .scaleEffect(scale)
        .foregroundColor(isPressed ? .red : .white)
    }
}

struct LikeButton_Previews: PreviewProvider {
       static var previews: some View {
        LikeButton(btnAction: () -> Void)
    }
}

3      

You would need to pass in a closure just as you'd do to the real LikeButton. If functionality doesn't matter in the preview, just pass an empty closure {}.

3      

@roosterboy Thanks

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.