下标的模糊使用 [英] Ambiguous use of subscript

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

问题描述

我有一个可扩展的表,其中自定义单元格在点击可见行时会出现或消失。单元格的数据存储在一个plist中并声明为NSMutableArray。

I have a expandable table with custom cells that appear or disappear when a visible row is tapped. The cell's data is stored in a plist and declared as a NSMutableArray.

我在下面的代码中得到了模糊使用下标错误,希望其他人遇到过这个并且知道修复。
我已经尝试了所有可能的选项,根据我必须添加的有限知识。

I get an 'ambiguous use of subscript' error in the following code and was hoping somebody else has encountered this and know the fix. I've tried all possible options, to my limited knowledge I must add.

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
            }


推荐答案

编译器告诉您它无法确定您要下标的对象类型实际上可以下载。你还没有提供任何关于你所谓的单元描述符的信息,因此很难确定问题是什么,但看起来你期望从<$ c下标的任何下标中返回一个字典。 $ c> indexOfTappedRow 查找。

The compiler is telling you it can't be sure the type of object you're subscripting can actually be subscripted. You haven't provided any information about what you call a "cell descriptor" so it's hard to say for sure what the problem is, but it looks like you're expecting a dictionary to be returned from whatever the subscript call with indexOfTappedRow looks up.

这可能是由于使用Cocoa集合,如 NSArray / NSMutableArray NSDictionary / NSMutableDictionary ,其中是 AnyObject 的容器(它丢失了关于它存储的确切内容的类型信息)。由于这些是免费桥接到Swift数组和字典,因此很容易更改您的模型(包括容器)以使用特定类型,以便您的容器可以明确地声明它们是code的数组 > CellDescriptor (与 NSArray 's一些随机类型的对象数组( AnyObject ))。通过这种方式,你不必做一些令人厌恶的事情,例如通过一些容易出错的字符串查找属性,你可能会在这里或那里输入错误(使用作为更糟糕的事情!,要求它有效或爆炸而不是真或假。)

This is probably due to using Cocoa collections like NSArray / NSMutableArray, and NSDictionary / NSMutableDictionary, which are containers of AnyObject (which loses type information about what exactly it's storing). Since these are toll-free bridged to Swift arrays and dictionaries, it's easy enough to change your model (including containers) to use specific types so that your containers can declare, definitively, that they're "an array of CellDescriptor" (versus NSArray's "an array of some random kind of object (AnyObject)"). This way, you don't have to do unsavory things like looking up properties by some error-prone string you may mistype here or there (which is made worse by your use of as!, which demands it be "valid or explode" versus "true or false").

将Swift的强类型系统视为可以利用的东西,而不是尽可能避免的事情。事情真的变得容易多了。

Treat Swift's strong type system as something to be taken advantage of rather than as something to avoid if possible. Things really do become a lot easier.

评论更新

在此case(因为PLIST是一个纯Cocoa对象图),你可以扩展你的代码,一次一步。那就是:

In this case (since a PLIST is a pure Cocoa object graph), you can just expand your code to take things one step at a time. That is:


  1. 将该部分作为字典数组获取(我们假设这个结构,因为它是一个已知结构的PLIST但是有永远是意外的空间......)

  2. 获取行的字典为? NSDictionary (提示:如果让...

  3. 获取的值isExpanded为? NSNumber (提示:如果让...

  4. 如果你已经走到这一步,请使用 NSNumber boolValue 。没有含糊之处。

  1. Get the section as an array of dictionaries (we assume this structure because it's a PLIST of a structure known to you but there's always room for the unexpected...)
  2. Get the dictionary for the row as? NSDictionary (hint: if let...)
  3. Get value for "isExpanded" key as? NSNumber (hint: if let...)
  4. If you've gotten this far, use the NSNumber's boolValue. No ambiguities.

这篇关于下标的模糊使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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