无法将'NSIndexPath'类型的值转换为预期的参数类型'NSIndexPath'

时间:2015-09-28 14:34:59

标签: ios swift nsindexpath

  

无法将'NSIndexPath'类型的值转换为预期的参数类型'NSIndexPath'

错误在self.collectionView?.moveItemAtIndexPath(obj[0] as! NSIndexPath, toIndexPath: obj[1] as! NSIndexPath) line

if let changes = self.objectChanges{
    self.collectionView?.performBatchUpdates({ () -> Void in
        for change in changes as NSArray as! [NSDictionary] {
            change.enumerateKeysAndObjectsUsingBlock { (keyObject:AnyObject!, obj:AnyObject!, stop) -> Void in
                let key = keyObject as! NSNumber
                let type = NSFetchedResultsChangeType(rawValue: key.unsignedLongValue)!
                switch (type)
                {
                    case .Insert:
                        self.collectionView?.insertItemsAtIndexPaths(obj as! ([NSIndexPath]))
                    case .Delete:
                        self.collectionView?.deleteItemsAtIndexPaths(obj as! ([NSIndexPath]))
                    case .Update:
                        self.collectionView?.reloadItemsAtIndexPaths(obj as! ([NSIndexPath]))
                    case .Move:
                        self.collectionView?.moveItemAtIndexPath(obj[0] as! NSIndexPath, toIndexPath: obj[1] as! NSIndexPath)
                    default:
                        break
                }
            }
        }
    }, completion: nil)
}

1 个答案:

答案 0 :(得分:2)

您正尝试将下标应用于obj AnyObject而不是Array。 您应该将obj投射到[NSIndexPath]

if let obj = obj as? [NSIndexPath] {
    switch (type) {
        case .Insert:
            self.collectionView?.insertItemsAtIndexPaths(obj)
        case .Delete:
            self.collectionView?.deleteItemsAtIndexPaths(obj)
        case .Update:
            self.collectionView?.reloadItemsAtIndexPaths(obj)
        case .Move:
            self.collectionView?.moveItemAtIndexPath(obj[0], toIndexPath: obj[1])
        default:
            break
    }
}