【问题标题】:Ambiguous Use of Subscript in SwiftSwift 中下标的模糊使用
【发布时间】:2016-02-12 01:01:49
【问题描述】:

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

【问题讨论】:

  • 你知道什么是下标吗?如果不是,则下标是访问集合元素的快捷方式。看看this 顺便说一下,你能不能把你currentQuestionDict 的类型贴出来了解一下上下文。
  • 类型?它基本上只是一个充满字符串的 NSDictionary。
  • 不要说“基本上”是什么。每个 Swift 变量都有一个声明,给它一个特定的类型。 显示currentQuestionDict 的声明。您问题的全部答案取决于该声明。隐藏它是愚蠢的:它就是答案。显示它。
  • 声明不是let currentQuestionDict = myQuestionsArray!.objectAtIndex(count)吗?
  • 好,我想这就解释了。

标签: arrays swift subscript


【解决方案1】:

问题是你使用的是 NSArray:

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 可以使用它。

【讨论】:

  • 优秀。不过,更重要的是要理解我所说的。
  • 您的回答中有一些让我有些困惑的地方。你说你不能为 AnyObject 下标,但是在你写了 `myQuestionsArray!.objectAtIndex(count) as! [NSObject:AnyObject]`,应用运行成功。
  • 因为在那之后这个故事中就没有 AnyObject 了。我们将其转换为 [NSObject:AnyObject],也就是字典 - 这是您可以下标的东西。
  • 哦。我想我明白你现在做了什么。
  • 你可能想阅读我关于 Swift 的书:apeth.com/swiftBook/ch04.html#_casting
【解决方案2】:

["Key"] 导致了此错误。新的 Swift 更新,你应该使用 objectForKey 来获得你的价值。在您的情况下,只需将您的代码更改为 ;

if let button1Title = currentQuestionDict.objectForKey("choice1") as? String {
    button1.setTitle("\(button1Title)", forState: UIControlState.Normal)
}

【讨论】:

    【解决方案3】:

    这是我用来解决错误的代码:

        let cell:AddFriendTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! AddFriendTableViewCell
    
        let itemSelection = items[indexPath.section] as! [AnyObject] //'items' is an array of NSMutableArrays, one array for each section
    
        cell.label.text = itemSelection[indexPath.row] as? String
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 2017-08-09
      • 1970-01-01
      • 2017-02-12
      相关资源
      最近更新 更多