         return rowCapacity - 1
     }
     
+    
     // Generator for implementing for..in iteration
-    func generate() -> AnyGenerator<T> {
-    
+    func generate() -> AnyGenerator<T>
+    {
+        
         let rowMax = rowCapacity
         let colMax = colCapacity
         

        var colIndex = 0
        
        // Passes function definition for next() on init with a closure
        return anyGenerator {
            if rowIndex < rowMax {
                if colIndex < colMax {
                    return self.getObjectFromMatrixAtRow(rowIndex, column: colIndex++)
                } else {
                    colIndex = 0
                    return self.getObjectFromMatrixAtRow(rowIndex++, column: colIndex)
                }
            } else {
                return nil

    
    
    // Double subscript notation is the only public interface: exampleMatrix[1,2]
    subscript(row:Int, col:Int) -> T
        {
        get {
            //index = (x:row, y:col)
            //updateWarning()
            assert(indexIsValidForRow(row, column: col), warning)
            return getObjectFromMatrixAtRow(row, column: col)
        }
        
        set {
            //index = (row, col)
            //updateWarning()
            assert(indexIsValidForRow(row, column: col),warning)
            setObjectInMatrixAtIndex(newValue, row: row, column: col)
        }
    }
    
    
    private func getObjectFromMatrixAtRow(row:Int, column col:Int) -> T
    {
        let nilWarning = "Nil returned from matrix at index \(row), \(col)"
        
        let obj = matrix.accessObjectAtRow(row, column: col) as? T
        assert(obj != nil, nilWarning)
        return obj!
    }
    
    
    private func setObjectInMatrixAtIndex(object:T, row:Int, column col:Int)
    {
        matrix.addObjectToMatrixAtIndex(object as AnyObject, row: row, column: col)
    }
    
    
    /* Debugging */
    private func updateWarning()
    {
        warning = "Index out of range - \(index) - row capacity: \(rowCapacity), col capacity: \(colCapacity)"
    }
    
    
    /* Debugging */
    private func indexIsValidForRow(row:Int, column col:Int) -> Bool
    {
        return (row >= 0 && row < rowCapacity) && (col >= 0 && col < colCapacity)
    }
}
