Hello, what is the proper way to delete completely subviews I added to my superview like this?
for row in (1...cellsPerRow) { /
for col in (1...cellsPerCol) { /
let gameBoardCell = UIView(frame: CGRectMake((cellLength + dividerWidth) * CGFloat(col - 1), (cellLength + dividerWidth) * CGFloat(row - 1), cellLength, cellLength))
gameBoard.addSubview(gameBoardCell)
gameBoardCells.append(gameBoardCell)
}
}
Later on I'm going to delete these subviews by doing:
for cell in gameBoardCells {
cell.removeFromSuperview()
cell = nil
}
gameBoardCells.removeAll()
The "cell = nil" line won't compile because cell was created effectively with a "let". How should I be deleting these subviews completely, ie undo destroy the objects created by UIView(...)? Thanks
Just delete that cell = nil line.
In the code you posted, you are only keeping two strong references to each cell. removeFromSuperview() takes care of one, and gameBoardCells.removeAll() takes care of the other. After that no references remain and the object will be deallocated.