Nicolas
Nicolas

Reputation: 35

Getting unknown errors on a custom SwiftUI view

While I was working on a custom SwiftUI view, I encountered random errors.

Errors: Cannot convert value of type '[String]' to expected argument type 'Binding'


Cannot convert value of type 'String' to expected argument type '(Binding<C.Element.Element>) throws -> Bool'

Please help me resolve these errors.

//  CustomTextFieldGuidelineView.swift
import SwiftUI

struct CustomTextFieldGuidelineView: View {
    
    var title: String
    var descriptions: [String]
    var paddingView: (top:CGFloat,right:CGFloat,bottom:CGFloat,left:CGFloat) = (10,10,10,10)
    @State var iconImage: String
    
    var body: some View {

        ZStack {
            
            CustomTextFieldGuidelineShapeView(arrowSize: 22, arrowPosition: 0.13, cornerRadius: 10)
                .stroke(Color.black, lineWidth: 1)
                .fill(Color.white)
            
            VStack(alignment: .leading) {
                
                CustomTextFieldGuidelineTitleView(title: title,
                                               titleColor: .black,
                                               backgroundColor: .white,
                                               font: Font.system(size: 20, weight: .bold))
                
                ForEach(descriptions, id: \.self) { description in **//Error**
                                    if description.contains("ⓘ") { **//Error**
                                        let parts = description.split(separator: "ⓘ")
                                        HStack {
                                            
                                            CustomImageWithTextView(
                                                imageName: iconImage,
                                                imageBackground: .white,
                                                title: String(parts[0]),
                                                fontTitle: Font.system(size: 13),
                                                imageFrame: (30, 30)
                                            )
                                            .fixedSize(horizontal: true, vertical: false)
                                            
                                            Image(systemName: "info.circle")
                                                .onTapGesture {
                                                    print("Info icon tapped")
                                                }
                                            
                                            if parts.count > 1 {
                                                Text(parts[1])
                                            }
                                        }

                                    } else {
                                        CustomImageWithTextView(
                                            imageName: iconImage,
                                            imageBackground: .white,
                                            title: description,
                                            font: Font.system(size: 13),
                                            imageFrame: (30, 30)
                                        )
                                    }
                                }
            }
            .padding(.leading,10)
            .padding(.top, 50)
            .padding(.bottom, 10)
        }
        
        .fixedSize(horizontal: false, vertical: true)
        .padding(.top, paddingView.top)
        .padding(.leading, paddingView.left)
        .padding(.bottom, paddingView.bottom)
        .padding(.trailing, paddingView.right)
    }
}

//  CustomImageWithTextView.swift
import SwiftUI

struct CustomImageWithTextView: View {
    var imageName: String?
    var title: String?
    var subTitle: String?
    var imageBackgroundColor: Color?
    var fontTitle: Font?
    var fontSubTitle: Font?
    var imageFrame: (width:Int, height:Int)
    var ColorTitle: Color?
    var ColorSubTitle: Color?
    
    
    var onTap: () -> Void = {}
    
    var body: some View {
        VStack(alignment: .leading) {
            HStack {
                if let imageName = imageName {
                    CustomImageView(name: imageName)
                        .aspectRatio(contentMode: .fill)
                        .frame( width: CGFloat(imageFrame.width), height: CGFloat(imageFrame.height))
                        .background(imageBackgroundColor ?? Color.green.opacity(0.5))
                        .clipShape(Circle())
                }
                
                
                VStack(alignment: .leading) {
                    if let descriptionBold = title {
                        Text(descriptionBold)
                            .foregroundColor(ColorTitle)
                            .font(fontTitle)
                            .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
                    }
                    if let description = subTitle {
                        Text(description)
                            .foregroundColor(ColorSubTitle)
                            .font(fontSubTitle)
                            .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
                    }
                }
            }
        }
        //.padding()
        .onTapGesture {
                    onTap()
                }
    }
}

Expected View: enter image description here

Thanks in advance.

Please correct my code.

Upvotes: 0

Views: 47

Answers (1)

Benzy Neez
Benzy Neez

Reputation: 21720

These errors are because, you have a mismatch between the names and order of the variables in CustomImageWithTextView and the parameters you are using to create the two instances of this view.

CustomImageWithTextView(
    imageName: iconImage,
    imageBackground: .white, // -> should be imageBackgroundColor
    title: String(parts[0]), // -> should be before imageBackgroundColor
    fontTitle: Font.system(size: 13),
    imageFrame: (30, 30)
)
CustomImageWithTextView(
    imageName: iconImage,
    imageBackground: .white,     // -> same issue as above
    title: description,          // -> same issue as above
    font: Font.system(size: 13), // -> not a valid parameter
    imageFrame: (30, 30)
)

Once you've resolved these issues, you might find the errors disappear. If not, try commenting out all other content (including the if-else switch) so that you can see if these views work in isolation.

Upvotes: 1

Related Questions