NSMutablearray move object from index to index

前端 未结 7 1021
无人共我
无人共我 2020-12-05 04:05

I have a UItableview with reordable rows and the data is in an NSarray. So how do I move an object in the NSMutablearray when the appropriate tableview delegate is called?

7条回答
  •  日久生厌
    2020-12-05 04:19

    Similar to Tomasz but with out of range error handling

    enum ArrayError: ErrorType {
        case OutOfRange
    }
    
    extension Array {
        mutating func move(fromIndex fromIndex: Int, toIndex: Int) throws {
            if toIndex >= count || toIndex < 0 {
                throw ArrayError.OutOfRange
            }
            insert(removeAtIndex(fromIndex), atIndex: toIndex)
        }
    }
    

提交回复
热议问题