gpt4 book ai didi

arrays - swift 2 : cannot cast value of type NSArray toNSString Dictionary?

转载 作者:行者123 更新时间:2023-11-30 13:58:16 25 4
gpt4 key购买 nike

我正在制作一个简单的应用程序来支持自闭症 child 的 parent 促进沟通。这应该很容易,因为我有一组之前项目的代码,但我坚持的是字典中的一组图像文件。我还是个新手,经过搜索后,但我认为有效的方法都不起作用。为什么?我需要做什么? (我什至放弃了数组,改成了字典,但还是有问题!)

import Foundation
import UIKit

struct WordList {

var placeCue: String?
var choiceImage: [UIImage] = []
var audio: [String] = []

init(index: Int){
let wordlistLibrary = WordlistLibrary().wordlists
let wordlistEntry = wordlistLibrary[index]

let iconName = wordlistEntry["choiceImage"]! as! String // When I run, this is the place to stuck. :(
choiceImage += [UIImage(named: iconName)!]

//choiceImage += wordlistEntry["choiceImage"] as! [UIImage]
placeCue = wordlistEntry["placeCue"] as! String!
audio += wordlistEntry["audio"] as! [String]
}
}
<小时/>

下面是我的字典的一部分。

struct WordlistLibrary {

var wordlists: [[String: AnyObject]] = [

[
"placeCue": "Audio0", // toothache
"choiceImage": ["WaterBottle.pdf", "Medicine.pdf", "Dentist.pdf", "SomethingElse.pdf"],
"audio": ["Audio16", "Audio17", "Audio14", "Audio21"]
],

[
"placeCue": "Audio1", // ear
"choiceImage": ["WaterBottle.pdf", "Medicine.pdf", "Nap.pdf", "SomethingElse.pdf"],
"audio": ["Audio16", "Audio17", "Audio18", "Audio21"]
]
]
}

最佳答案

您的choiceImage[String]。它不是字符串

所以将其更改为

let iconName = wordlistEntry["choiceImage"]! as! [String]
choiceImage = iconName.map{ UIImage(named: $0)! }

关于arrays - swift 2 : cannot cast value of type NSArray toNSString Dictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33325939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com