在斯威夫特下标使用不明确 [英] Ambiguous Use of Subscript in Swift

查看:183
本文介绍了在斯威夫特下标使用不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在我的雨燕code得到一个错误暧昧使用下标。我不知道是什么导致了这个错误。它只是随机弹出。这里是我的code:

 如果让PATH = NSBundle.mainBundle()pathForResource(MusicQuestions,ofType:plist中)。{
    myQuestionsArray =的NSArray(contentsOfFile:路径)
}VAR计数:INT = 1
让currentQuestionDict = myQuestionsArray!.objectAtIndex(计数)如果让button1Title = currentQuestionDict [choice1]作为?字符串{
    button1.setTitle(\\(button1Title),forState:UIControlState.Normal)
}如果让button2Title = currentQuestionDict [选择2]作为?字符串{
    button2.setTitle(\\(button2Title),forState:UIControlState.Normal)
}如果让button3Title = currentQuestionDict [choice3]作为?字符串{
    button3.setTitle(\\(button3Title),forState:UIControlState.Normal)
}
如果让button4Title = currentQuestionDict [choice4]作为?字符串{
    button4.setTitle(\\(button4Title),forState:UIControlState.Normal)
}如果让问题= currentQuestionDict [问题]作为?串!{
    questionLabel.text =\\(问题)
}


解决方案

的问题是,你正在使用的NSArray:

  myQuestionsArray =的NSArray(contentsOfFile:路径)

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

 让currentQuestionDict = myQuestionsArray!.objectAtIndex(计数)

...斯威夫特没有类型信息,并具有使 currentQuestionDict 的AnyObject。但是你不能下标为AnyObject,所以前pressions像 currentQuestionDict [choice1] 不能编译。

的解决方案是使用夫特类型。如果你知道是什么 currentQuestionDict 还真是,键入它作为类型。最起码,因为你似乎认为这是一本字典,使其之一;键入它,用 [NSObject的:AnyObject] (如果可能的话更具体)。您可以通过几种方式做到这一点;一种方法是通过铸造,当你创建变量:

 让currentQuestionDict =
    myQuestionsArray!.objectAtIndex(数)为! [NSObject的:AnyObject]

总之,千万不要用NSArray的NSDictionary的和如果你能避免它(你通常能避免它)。如果从Objective-C的接收一个,键入它,它到底是什么,让雨燕可以使用它。

I keep getting an error of "ambiguous use of subscript," in my Swift code. I don't know what's causing this error. It just randomly popped up. Here's my code:

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)"
}

解决方案

The problem is that you are using NSArray:

myQuestionsArray = NSArray(contentsOfFile: path)

This means that myQuestionArray is an NSArray. But an NSArray has no type information about its elements. Thus, when you get to this line:

let currentQuestionDict = myQuestionsArray!.objectAtIndex(count)

...Swift has no type information, and has to make currentQuestionDict an AnyObject. But you can't subscript an AnyObject, so expressions like currentQuestionDict["choice1"] cannot compile.

The solution is to use Swift types. If you know what currentQuestionDict really is, type it as that type. At the very least, since you seem to believe it is a dictionary, make it one; type it as [NSObject:AnyObject] (and more specific if possible). You can do this in several ways; one way is by casting when you create the variable:

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

In short, never use NSArray and NSDictionary if you can avoid it (and you can usually avoid it). If you receive one from Objective-C, type it as what it really is, so that Swift can work with it.

这篇关于在斯威夫特下标使用不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆