Deepak Thakur
Deepak Thakur

Reputation: 3691

'NSFastEnumerator.Element' (aka 'Any') has no subscript members

I was going through this appcoda blog and came across a function where I have to get indices of visible rows. I am learning and implementing Swift 3 / Xcode 8. I get No subscript members error for the following function.

func getIndicesOfVisibleRows() {
    visibleRowsPerSection.removeAll()

    for currentSectionCells in cellDescriptors {
        var visibleRows = [Int]()

        for row in 0...((currentSectionCells as! [[String: AnyObject]]).count - 1) {
            if currentSectionCells[row]["isVisible"] as! Bool == true { //Get compile time error here
                visibleRows.append(row)
            }
        }

        visibleRowsPerSection.append(visibleRows)
    }
}

How do I get object of currentSectionCells array whose object for key is "isVisible" here?

Upvotes: 0

Views: 2361

Answers (4)

Kiran Jasvanee
Kiran Jasvanee

Reputation: 6554

One more thing you can do is, create your cellDescriptors as a specific swift array as below.

var cellDescriptors: [[[String: Any]]]!  

and load your cellDescriptor from .plist file as below.

cellDescriptors = NSMutableArray(contentsOfFile: path)! as NSArray as! [[[String: Any]]]  

Now, your code (Mentioned on question) will work as it is, without any changes!

Upvotes: 0

STOP
STOP

Reputation: 21

func getIndicesOfVisibleRows() { visibleRowsPerSection.removeAll()

    for currentSectionCells in cellDescriptors {
        print(currentSectionCells)
        var visibleRows = [Int]()

        for row in 0...((currentSectionCells as AnyObject).count - 1) {

            if (currentSectionCells as AnyObject).objectAt(row)["isVisible"] as! Bool == true {
                visibleRows.append(row)
            }
        }

        visibleRowsPerSection.append(visibleRows)
    }
}

Upvotes: 0

Nirav D
Nirav D

Reputation: 72410

You need to specify the type of your array cellDescriptors to [[[String:Any]]] like this way.

for currentSectionCells in cellDescriptors.objectEnumerator().allObjects as! [[[String:Any]]]{
    var visibleRows = [Int]()

    for row in 0..<currentSectionCells.count {
        if currentSectionCells[row]["isVisible"] as! Bool == true { 
            visibleRows.append(row)
        }
    }
    visibleRowsPerSection.append(visibleRows)
}

Upvotes: 3

Bista
Bista

Reputation: 7893

Try this:

for currentSectionCells in cellDescriptors as! [[String: AnyObject]] {

And

for row in 0...currentSectionCells.count - 1 {

Upvotes: 0

Related Questions