Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了阵列 – Swift中下标的模糊使用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

在我的Swift代码中,我不断收到“模糊使用下标”的错误。我不知道是什么导致这个错误。它随机弹出。这是我的代码: if let path = NSBundle.mainBundle().pathForresource("MusicQuestions", ofType: "plist") { myQuestionsArray = NSArray(contentsOfFile: path) }
@H_801_6@
@H_801_6@
在我的Swift代码中,我不断收到“模糊使用下标”的错误。我不知道是什么导致这个错误。它随机弹出。这是我的代码
if let path = NSBundle.mainBundle().pathForresource("MusicQuestions",ofType: "plist") {
    myQuestionsArray = NSArray(contentsOfFile: path)
}

var count:Int = 1
let currentQuestionDict = myQuestionsArray!.objectATindex(count)

if let button1title = currentQuestionDict["choice1"] as? String {
    button1.settitle("\(button1titlE)",forState: UIControlState.Normal)
}

if let button2title = currentQuestionDict["choice2"] as? String {
    button2.settitle("\(button2titlE)",forState: UIControlState.Normal)
}

if let button3title = currentQuestionDict["choice3"] as? String {
    button3.settitle("\(button3titlE)",forState: UIControlState.Normal)
}
if let button4title = currentQuestionDict["choice4"] as? String {
    button4.settitle("\(button4titlE)",forState: UIControlState.Normal)
}

if let question = currentQuestionDict["question"] as? String!{
    questionLabel.text = "\(question)"
}
问题是你正在使用NSArray: @H_28_15@myQuestionsArray = NSArray(contentsOfFile: path)

这意味着myQuestionArray是一个NSArray。但是NSArray没有关于它的元素的类型信息。因此,当你到达这一行时:

let currentQuestionDict = myQuestionsArray!.objectATindex(count)

… Swift没有类型信息,并且必须使currentQuestionDict成为AnyObject。但是你不能下标AnyObject,所以表达式如currentQuestionDict [“choice1”]无法编译。

解决方案是使用Swift类型。如果你知道currentQuestionDict真的是什么,请键入它作为该类型。至少,因为你似乎相信它是一本字典,使它成为一个;键入它作为[NSObject:AnyObject](如果可能,更具体)。你可以通过几种方法来做到这一点;一种方式是通过在创建变量时进行转换:

let currentQuestionDict = 
    myQuestionsArray!.objectATindex(count) as! [NSObject:AnyObject]

简而言之,如果你可以避免使用NSArray和NSDictionary,你可以避免使用它。如果您从Objective-C收到一个,请将其输入它的真实内容,以便Swift可以使用它。

大佬总结

以上是大佬教程为你收集整理的阵列 – Swift中下标的模糊使用全部内容,希望文章能够帮你解决阵列 – Swift中下标的模糊使用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
猜你在找的Swift相关文章