Search code examples
arraysswiftmachine-learningcoremlcreateml

assign array in swift machine learning Cannot assign value of type '[[String]]' to type 'String?'


I am using machine learning and image detection in my swift app but instead of printing the results in my textView of what the model thinks the image might be I want to replace it with an array i created but every-time i try it gives me an error message saying Cannot assign value of type '[[String]]' to type 'String?'

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var choosePhotoLib: UIButton!
@IBOutlet weak var textView: UITextView!

var graduation = ["Living your Best Life", "Happiness looks good on you","ive been growing the business", "there are things i want in this life, things i gotta do things i want"]

let imagePicker = UIImagePickerController()

override func viewDidLoad() {
    super.viewDidLoad()
    imagePicker.delegate = self
    imagePicker.sourceType = .camera
    imagePicker.allowsEditing = false

}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if  let userPickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
    imageView.image = userPickedImage
        guard let ciimage = CIImage(image: userPickedImage) else {
            fatalError("Could Not Convert To CIImage")
        }
       detect(image: ciimage)
    }


imagePicker.dismiss(animated: true, completion: nil)

}

func detect(image: CIImage) {
    guard let model = try? VNCoreMLModel(for: Inceptionv3__1__1().model) else {
        fatalError("Could Not Load Core ML Model")
    }

    let request = VNCoreMLRequest(model: model) { (request, error) in
        guard let results = request.results as? [VNClassificationObservation] else {
            fatalError("Could Not Get Reuest")
        }
        if let firstResult = results.first {
            if firstResult.identifier.contains("Graduation") {
                self.textView.text = [graduation] WHERE THE ERROR OCCURS

Solution

  • If you just want one of the texts in graduation to be displayed, do the following: self.textView.text = graduation[0] (0 is the first line)

    if you want all of them you'll have to loop through them all

    var allTexts = ""
    for text in graduation {
        allTexts.append(text)
    }
    self.textView.text = allTexts
    

    this will put the strings together one after another, if you want spaces or line breaks between them you'll have to do that as well. line break can be added by putting in \n in a string