Home > Software design >  Swift UI - Closure containing control flow statement cannot be used with result builder 'Comman
Swift UI - Closure containing control flow statement cannot be used with result builder 'Comman

Time:02-01

I'm trying to check if my array contains the machine learning model output, but its giving me this weird error, and I don't understand it.

Here's my code

Image Detector class:

import SwiftUI

class ImageDetector: ObservableObject {
    @Published private var detector = Detector()
       
       var imageClass: String? {
           detector.results
       }
           
       // MARK: Intent(s)
       func detect(uiImage: UIImage) {
           guard let ciImage = CIImage (image: uiImage) else { return }
           detector.detect(ciImage: ciImage)
           
       }}

Content View:

    @ObservedObject var detector: ImageDetector
    
    
    let collectionArr = ["Test", "test2", "test3", "test4"]

    
    var body: some View {
        VStack {

            var mlOutput = detector.imageClass
        
            Group{
                
                // Outputing the information of the respective Item
                if collectionArr.contains(mlOutput) {
                    HStack{
                        Text("")
                        .font(.caption)
                        Text(mlOutput)
                            .bold()
                    }
                }
                    else {
                        HStack{
                            Text("This is not in the array")
                            .font(.caption)
                           
                    } 
                }
            }

CodePudding user response:

Since .contains returns a Bool, you don't need to provide a binding to a variable (which is used in cases where you have a statement returning an Optional value).

//remove var mlOutput... line here
Group{
  if let mlOutput = detector.imageClass, collectionArr.contains(mlOutput) {
    HStack{
      Text("").font(.caption)
      Text(mlOutput).bold()
    }
  } else {
    HStack {
      Text("This is not in the array").font(.caption)
    } 
  }
}

This is assuming that mlOuput (which maybe is a typo for mlOutput?) is a String

  •  Tags:  
  • Related