Saturday, October 14, 2023
HomeiOS DevelopmentThe compiler is unable to type-check this expression in cheap time; attempt...

The compiler is unable to type-check this expression in cheap time; attempt breaking apart the expression into distinct sub-expressions IOS Swift


Hello the stackOverFlow Swift group, I’m making a a number of picture picker following the tutorial on this video: https://www.youtube.com/watch?v=SfcJewIgF7Q. Nonetheless, on my struct CustomPicker View, I all the time getting this bug “The compiler is unable to type-check this expression in cheap time; attempt breaking apart the expression into distinct sub-expressions”. I attempted troubleshoot by eradicating the road one after the other and I seen if I take away

ForEach(i..<i+3, id: .self){j in

                                    HStack{
                                        if j < self.knowledge.depend{
                                            Card(knowledge: self.knowledge[j], chosen: self.$chosen)
                                        }
                                    }
                            }

Then the error might be gone, the complete code is proven under

import SwiftUI
import Images

struct Dwelling: View{
    @State var chosen : [UIImage] = []
    @State var present = false
    var physique: some View{
        ZStack{
            Shade.black.opacity(0.07).edgesIgnoringSafeArea(.all)
            VStack{
                if !self.chosen.isEmpty{
                    ScrollView(.horizontal, showsIndicators: false){
                        HStack(spacing: 20){
                            ForEach(self.chosen, id: .self){i in
                                Picture(uiImage: i)
                                    .resizable()
                                    .body(width: UIScreen.principal.bounds.width - 40, peak: 250)
                                    .cornerRadius(15)
                            }
                        }
                        .padding(.horizontal, 20)
                    }
                }
            }
            
            Button(motion: {
                self.chosen.removeAll()
                self.present.toggle()
            }){
                Textual content("Picture Picker")
                    .foregroundColor(.white)
                    .padding(.vertical, 10)
                    .body(width: UIScreen.principal.bounds.width/2)
            }.background(Shade.crimson).clipShape(Capsule()).padding(.prime, 25)
        }
        if self.present {
            CustomPicker(chosen: self.$chosen, present: self.$present)
        }
    }
}

struct CustomPicker: View {
    @Binding var chosen:[UIImage]
    @State var knowledge: [Images] = []
    @State var grid: [Int] = []
    @Binding var present : Bool
    @State var disabled = false
    
    var physique: some View {
        GeometryReader{_ in
            VStack{
                if !self.grid.isEmpty{
                    HStack{
                        Textual content("Choose a Picture").fontWeight(.daring)
                        Spacer()
                    }
                    .padding(.main)
                    .padding(.prime)
                    
                    ScrollView(.vertical, showsIndicators: false){
                        VStack(spacing: 20){
                            ForEach(self.grid, id: .self){ i in
                                HStack(spacing:8){
                                    ForEach(i..<i+3, id: .self){j in
                                        HStack{
                                            if j < self.knowledge.depend{
                                                Card(knowledge: self.knowledge[j], chosen: self.$chosen)
                                            }
                                        }
                                    }
                                    if self.knowledge.depend % 3 != 0 && i == self.grid.final!{
                                        Spacer()
                                    }
                                }
                                .padding(.main, (self.knowledge.depend % 3 != 0 && i == self.grid.final!) ? 15:0)
                            }
                        }
                    }
                    Button(motion:{
                        self.present.toggle()
                    }){
                        Textual content("Choose")
                            .foregroundColor(.white)
                            .padding(.vertical, 10)
                            .body(width: UIScreen.principal.bounds.width/2)
                    }
                    .background(Shade.crimson.opacity((self.chosen.depend != 0) ? 1 : 0.5))
                    .clipShape(Capsule())
                    .padding(.backside, 25)
                    .disabled((self.chosen.depend != 0) ? false : true)
                }
                else {
                    if self.disabled{
                        Textual content("Please allow storage entry")
                    }else{
                        Indicator()
                    }
                }
            }
            .body(width: UIScreen.principal.bounds.width - 40, peak: UIScreen.principal.bounds.peak / 1.5)
            .background(Shade.white)
            .cornerRadius(12)
        }
        .background(Shade.black.opacity(0.1).edgesIgnoringSafeArea(.all)
                        .onTapGesture {
            self.present.toggle()
        })
        .onAppear{
            PHPhotoLibrary.requestAuthorization{(standing) in
                if standing == .licensed{
                    self.getAllImages()
                    self.disabled = false
                }
                else {
                    print("not licensed")
                    self.disabled = true
                }
            }
        }
    }
    
    func getAllImages(){
        let req = PHAsset.fetchAssets(with: .picture, choices: .none)
        DispatchQueue.international(qos: .background).async {
            req.enumerateObjects {(asset, _, _) in
                let choices = PHImageRequestOptions()
                choices.isSynchronous = true
                
                PHCachingImageManager.default().requestImage(for: asset, targetSize: .init(), contentMode: .default, choices: choices){(picture, _) in
                    let data1 = Photos(picture: picture!, chosen: false)
                    self.knowledge.append(data1)
                }
            }
            if req.depend == self.knowledge.depend{
                self.getGrid()
            }
        }
    }
    
    func getGrid(){
        for i in stride(from: 0, to: self.knowledge.depend, by: 3){
            self.grid.append(i)
        }
    }
}

struct Photos {
    var picture : UIImage
    var chosen : Bool
}

struct Card : View {
    @State var knowledge : Photos
    @Binding var chosen: [UIImage]
    var physique : some View {
        ZStack{
            Picture(uiImage: self.knowledge.picture).resizable()
            
            if self.knowledge.chosen{
                ZStack{
                    Shade.black.opacity(0.5)
                    Picture(systemName: "checkmark")
                        .resizable()
                        .body(width: 30, peak: 30)
                        .foregroundColor(.white)
                }
            }
        }.body(width: (UIScreen.principal.bounds.width - 80)/3, peak: 90)
            .onTapGesture {
                if !self.knowledge.chosen {
                    self.knowledge.chosen = true
                    self.chosen.append(self.knowledge.picture)
                }
                else {
                    for i in 0..<self.chosen.depend{
                        if self.chosen[i] == self.knowledge.picture{
                            self.chosen.take away(at: i)
                            self.knowledge.chosen = false
                            return
                        }
                    }
                }
            }
    }
}


struct Indicator : UIViewRepresentable {
    func makeUIView(context: Context) -> UIActivityIndicatorView {
        let view = UIActivityIndicatorView(fashion: .giant)
        view.startAnimating()
        return view
    }
    
    func updateUIView(_ uiView: UIActivityIndicatorView, context: Context) {
    }
}

I’ve completely no thought why the error might be solved by eradicating these strains. Could I ask why this error happen and the way I can forestall it? Thanks



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments