Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

arrays - Ambiguous Use of Subscript in Swift

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)"
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...