SWIFT - Create/Change 长度等于 1 的 NSIndexPath
SWIFT - Create/Change NSIndexPath with length equal to 1
所以现在,indexPath 的长度是 2。有没有办法可以将它更改为 1?我是否必须手动创建长度为 1 的不同 NSIndexPath?如果是这样,我将如何去做。
提前致谢
您还没有真正描述您要做什么以及为什么要尝试,但是
myIndexPath = myIndexPath.indexPathByRemovingLastIndex()
您必须创建一个新的 NSIndexPath
。如果您需要删除特定值,则必须使用 Swift
中的指针
let originalPath = NSIndexPath(indexes: [1,2,3], length: 3)
var indexes = UnsafeMutablePointer<Int>.alloc(originalPath.length)
originalPath.getIndexes(indexes) // This extracts values from NSIndexPath
var newValues = Array<Int>()
// Iterate over the pointer array
for val in UnsafeBufferPointer(start: indexes, count: originalPath.length) {
// Determine values which you want in the new NSIndexPath
newValues.append(val)
}
let newPath = NSIndexPath(indexes: newValues, length: newValues.count)
所以现在,indexPath 的长度是 2。有没有办法可以将它更改为 1?我是否必须手动创建长度为 1 的不同 NSIndexPath?如果是这样,我将如何去做。
提前致谢
您还没有真正描述您要做什么以及为什么要尝试,但是
myIndexPath = myIndexPath.indexPathByRemovingLastIndex()
您必须创建一个新的 NSIndexPath
。如果您需要删除特定值,则必须使用 Swift
let originalPath = NSIndexPath(indexes: [1,2,3], length: 3)
var indexes = UnsafeMutablePointer<Int>.alloc(originalPath.length)
originalPath.getIndexes(indexes) // This extracts values from NSIndexPath
var newValues = Array<Int>()
// Iterate over the pointer array
for val in UnsafeBufferPointer(start: indexes, count: originalPath.length) {
// Determine values which you want in the new NSIndexPath
newValues.append(val)
}
let newPath = NSIndexPath(indexes: newValues, length: newValues.count)