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

标签 arrays swift dictionary

我正在制作一个简单的应用程序来支持自闭症 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/

相关文章:

swift - Eureka rangeSliderRow

c# - 这是在多线程方法中安全使用 Dictionary 吗?

python - 将值移出对象

arrays - 如何检查数组中有多少真实项和多少虚假项(SWIFT3)

swift - 使用 Alamofire 进行身份验证的请求

swift - 保存在 userdefaults 中会使应用程序崩溃

c# - 返回类型为 Task<Dictionary<string, string>> 的任务

python - 如何在 Python 中循环遍历两个字典

arrays - 如何在 Haskell 中滚动快速 BVH 表示

java - 面试问题 - 在排序数组 X 中搜索索引 i 使得 X[i] = i