1

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
Saurabh
  • 745
  • 1
  • 9
  • 33
  • Gradiantion is an Array of String. So when you do `[graduation]`, then it's an array (with only one object) of array of string. And `self.textView.text` wants a `String`, nothing else. Did you meant `graduation.joined(separator:"\n")` (or something like that)? What do you want to put in your textView exactly? – Larme Aug 10 '18 at 07:55
  • thank you for taking the time to respond Larme.I want my text view to contain the graduation array i created – Lawson Falomo Aug 10 '18 at 07:59
  • Are you trying to display all the array contents or a single element from the array in textView? – Saurabh Aug 10 '18 at 08:23

1 Answers1

1

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

lindanordstrom
  • 361
  • 2
  • 10