在快速机器学习中分配数组无法将类型[[[String]]'的值分配给类型'String?'

时间:2018-08-10 07:48:02

标签: arrays swift machine-learning coreml createml

我在我的swift应用程序中使用了机器学习和图像检测功能,但不是在textView中打印模型认为图像可能是什么的结果,我想将其替换为我创建的数组,但是每个我尝试它的时间会给我一条错误消息,提示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

1 个答案:

答案 0 :(得分:1)

如果只想显示毕业文本之一,请执行以下操作: self.textView.text = graduation[0](第一行为0)

如果要全部使用它们,则必须全部遍历

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

这将把一个接一个的字符串放在一起,如果要在它们之间使用空格或换行符,则也必须这样做。 可以通过在字符串中放入\n来添加换行符