1
我在Swift写了一个QuizApp,这段代码让我疯狂。从'NSMutableArray'投射到不相关的类型'[String]'总是失败
我有我UIViewController类之前声明下列变量:
var questionsList:[String] = []
var answersList:[NSMutableArray] = []
我做阅读的.plist文件加载它的内容:
let dictRoot = NSDictionary(contentsOfFile: NSBundle.mainBundle().pathForResource("filename", ofType: "plist")!)
questionsList = (dictRoot?.allKeys)! as! [String]
answersList = (dictRoot?.allValues)! as! [NSMutableArray]
我有一个generateRandomQuestion函数是这样的:
func generateRandomQuestion() {
let randomQuestionIndex = Int(arc4random() % UInt32(questionsList.count))
randomQuestion = "\(questionsList[randomQuestionIndex])"
answersToRandomQuestion = (answersList[randomQuestionIndex]) as! [String]
不幸的是,铸造到NSMutableArray到[字符串]总是失败...
任何提示如何将其转换而不会搞乱?
(我试过flatMap,但它不工作...)
任何帮助,不胜感激!
不知道怎么说谢谢。你救了我的一天!万分感激! p.s.试图投票,但没有足够的声誉,使其可见:-( – Alex
但我认为你能够接受答案与代表1 – vadian
真! – Alex