下标使用不明确
Ambiguous use of subscript
我有一个可扩展的 table,带有自定义单元格,当点击可见行时,这些单元格会出现或消失。单元格的数据存储在 plist 中并声明为 NSMutableArray.
我在以下代码中遇到 'ambiguous use of subscript' 错误,希望其他人遇到过这个问题并知道修复方法。
我已经尝试了所有可能的选项,据我所知,我必须添加。
var cellDescriptors: NSMutableArray!
func getCellDescriptorForIndexPath(indexPath: NSIndexPath) -> [String: AnyObject] {
let indexOfVisibleRow = visibleRowsPerSection[indexPath.section][indexPath.row]
let cellDescriptor = cellDescriptors[indexPath.section][indexOfVisibleRow] as! [String: AnyObject]
return cellDescriptor
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexOfTappedRow = visibleRowsPerSection[indexPath.section][indexPath.row]
if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == true { // Ambiguous use of subscript error
var shouldExpandAndShowSubRows = false
if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == false { // Ambiguous use of subscript error
// In this case the cell should expand.
shouldExpandAndShowSubRows = true
}
编译器告诉您它不能确定您下标的对象类型实际上是否可以被下标。您没有提供任何关于您所谓的 "cell descriptor" 的信息,因此很难确定问题出在哪里,但看起来您希望从任何带有 [= 的下标调用中返回字典10=] 向上看。
这可能是由于使用了 Cocoa 集合,例如 NSArray
/ NSMutableArray
和 NSDictionary
/ NSMutableDictionary
,它们是 [=15] 的容器=](它丢失了关于它到底存储了什么的类型信息)。由于这些是免费桥接到 Swift 数组和字典,因此很容易更改您的模型(包括容器)以使用特定类型,以便您的容器可以明确地声明它们是 "an array of CellDescriptor
" (相对于 NSArray
的 "an array of some random kind of object (AnyObject
)")。这样,您就不必做一些令人讨厌的事情,例如通过一些容易出错的字符串查找属性,您可能会在这里或那里输入错误(使用 as!
会变得更糟,它要求它是 "valid or explode" 与 "true or false").
将Swift的强类型系统视为可以利用的东西,而不是尽可能避免的东西。事情真的变得简单多了。
评论更新
在这种情况下(因为 PLIST 是一个纯粹的 Cocoa 对象图),您可以扩展您的代码以一次采取一个步骤。即:
- 将部分作为字典数组获取(我们假定此结构是因为它是您已知结构的 PLIST,但总有空间容纳意外...)
- 获取行
as? NSDictionary
的字典(提示:if let...
)
- 获取
"isExpanded"
键的值 as? NSNumber
(提示:if let...
)
- 如果您已经走到这一步,请使用
NSNumber
的 boolValue
。没有歧义。
Cor,我假设你现在已经解决了这个问题,但在找到解决方案后,我想我会提请你注意:
func getCellDescriptorForIndexPath(indexPath: NSIndexPath) -> [String: AnyObject] {
let indexOfVisibleRow = visibleRowsPerSection[indexPath.section][indexPath.row]
// HERE IS WHAT YOU WANT:
let cellDescriptor = (cellDescriptors[indexPath.section] as! NSMutableArray)[indexOfVisibleRow] as! [String: AnyObject]
return cellDescriptor
}
if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == true {
var shouldExpandAndShowSubRows = false
if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == false { // Ambiguous use of subscript error
// In this case the cell should expand.
shouldExpandAndShowSubRows = true
}
}
应替换为:
if **((cellDescriptors[indexPath.section] as! NSMutableArray)[indexOfTappedRow] as AnyObject)["isExpandable"] as! Bool** == true {
var shouldExpandAndShowSubRows = false
if ((cellDescriptors[indexPath.section] as! NSMutableArray)[indexOfTappedRow] as AnyObject)["isExpanded"] as! Bool == false {
// In this case the cell should expand.
shouldExpandAndShowSubRows = true
}
}
我有一个可扩展的 table,带有自定义单元格,当点击可见行时,这些单元格会出现或消失。单元格的数据存储在 plist 中并声明为 NSMutableArray.
我在以下代码中遇到 'ambiguous use of subscript' 错误,希望其他人遇到过这个问题并知道修复方法。 我已经尝试了所有可能的选项,据我所知,我必须添加。
var cellDescriptors: NSMutableArray!
func getCellDescriptorForIndexPath(indexPath: NSIndexPath) -> [String: AnyObject] {
let indexOfVisibleRow = visibleRowsPerSection[indexPath.section][indexPath.row]
let cellDescriptor = cellDescriptors[indexPath.section][indexOfVisibleRow] as! [String: AnyObject]
return cellDescriptor
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexOfTappedRow = visibleRowsPerSection[indexPath.section][indexPath.row]
if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == true { // Ambiguous use of subscript error
var shouldExpandAndShowSubRows = false
if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == false { // Ambiguous use of subscript error
// In this case the cell should expand.
shouldExpandAndShowSubRows = true
}
编译器告诉您它不能确定您下标的对象类型实际上是否可以被下标。您没有提供任何关于您所谓的 "cell descriptor" 的信息,因此很难确定问题出在哪里,但看起来您希望从任何带有 [= 的下标调用中返回字典10=] 向上看。
这可能是由于使用了 Cocoa 集合,例如 NSArray
/ NSMutableArray
和 NSDictionary
/ NSMutableDictionary
,它们是 [=15] 的容器=](它丢失了关于它到底存储了什么的类型信息)。由于这些是免费桥接到 Swift 数组和字典,因此很容易更改您的模型(包括容器)以使用特定类型,以便您的容器可以明确地声明它们是 "an array of CellDescriptor
" (相对于 NSArray
的 "an array of some random kind of object (AnyObject
)")。这样,您就不必做一些令人讨厌的事情,例如通过一些容易出错的字符串查找属性,您可能会在这里或那里输入错误(使用 as!
会变得更糟,它要求它是 "valid or explode" 与 "true or false").
将Swift的强类型系统视为可以利用的东西,而不是尽可能避免的东西。事情真的变得简单多了。
评论更新
在这种情况下(因为 PLIST 是一个纯粹的 Cocoa 对象图),您可以扩展您的代码以一次采取一个步骤。即:
- 将部分作为字典数组获取(我们假定此结构是因为它是您已知结构的 PLIST,但总有空间容纳意外...)
- 获取行
as? NSDictionary
的字典(提示:if let...
) - 获取
"isExpanded"
键的值as? NSNumber
(提示:if let...
) - 如果您已经走到这一步,请使用
NSNumber
的boolValue
。没有歧义。
Cor,我假设你现在已经解决了这个问题,但在找到解决方案后,我想我会提请你注意:
func getCellDescriptorForIndexPath(indexPath: NSIndexPath) -> [String: AnyObject] {
let indexOfVisibleRow = visibleRowsPerSection[indexPath.section][indexPath.row]
// HERE IS WHAT YOU WANT:
let cellDescriptor = (cellDescriptors[indexPath.section] as! NSMutableArray)[indexOfVisibleRow] as! [String: AnyObject]
return cellDescriptor
}
if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == true {
var shouldExpandAndShowSubRows = false
if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == false { // Ambiguous use of subscript error
// In this case the cell should expand.
shouldExpandAndShowSubRows = true
}
}
应替换为:
if **((cellDescriptors[indexPath.section] as! NSMutableArray)[indexOfTappedRow] as AnyObject)["isExpandable"] as! Bool** == true {
var shouldExpandAndShowSubRows = false
if ((cellDescriptors[indexPath.section] as! NSMutableArray)[indexOfTappedRow] as AnyObject)["isExpanded"] as! Bool == false {
// In this case the cell should expand.
shouldExpandAndShowSubRows = true
}
}