在 Swift 中进行机器学习时无法将类型为 ‘[[String]]’ 的值赋值给类型为 ‘String?’ 的变量

我在我的 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

回答:

如果您只想显示 graduation 中的一个文本,可以这样做:self.textView.text = graduation[0](0 是第一行)

如果您想显示所有文本,您需要遍历它们

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

这将把字符串一个接一个地连接起来,如果您想在它们之间添加空格或换行符,您也需要这样做。可以通过在字符串中添加 \n 来添加换行符

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注