Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to initialise Binding var in initialiser

In my below code , no matter what I do I always get error for the expression that I use for imageData in init, I use it as a wrapper for Binding , I have tried _imageData = Binding(projectedValue: $imageData) but I keep getting error, how can I satisfy the Binding in initialiser, kindly guide, thanks

import SwiftUI

struct AddView: View {
    @Binding var imageData: UIImage?   <----------Use
    @Environment(\.dismiss) var dismiss
    @State var friendName: String = ""
  
    let savePath = FileManager.documentsDirectory.appendingPathComponent("SavedFriends")
    
    var save: (Friend) -> Void
    
    init(save: @escaping  (Friend) -> Void) {
       
        self.save = save
        _friendName = State(initialValue: "")
         _imageData = Binding(projectedValue: $imageData)  <----error
    }
    
    var body: some View {
        NavigationView {
            VStack {
                TextField("your friends name", text: $friendName)
                Image(uiImage: imageData!) <--------used here
                    .resizable()
                  //  .frame(width: 40, height: 40)
                    .scaledToFit()
                }
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("Save") {
                        let newFriend = Friend(picName: friendName, id: UUID())
                            save(newFriend)
                            dismiss()
                    }
                    .padding()
                }

                ToolbarItem(placement: .navigationBarLeading) {
                    Button("Cancel") {
                        dismiss()
                    }
                    .padding()
                }
            }
            }
        }
   
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When you provide initialiser you have to initialise everything in it, ie. including binding.

Here is fixed variant

init(imageData: Binding<UIImage?>, save: @escaping  (Frient) -> Void) {

    self.save = save
    _friendName = State(initialValue: "")
    _imageData = imageData
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading