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

标签 ios swift nsindexpath

Cannot convert value of type 'NSIndexPath' to expected argument type 'NSIndexPath'

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

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)
}

最佳答案

您正在尝试将下标应用于 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
    }
}

关于ios - 无法将类型 'NSIndexPath' 的值转换为预期的参数类型 'NSIndexPath',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32825647/

相关文章:

ios - 如何在 UICollectionView 中加载更多对象

swift - ARKit – 如何显示来自放置在 SCNPlane 上的虚拟 SCNCamera 的提要?

objective-c - UITableView 的 indexPathForCell : returns always 0 as indexPath. 行

ios - 如何获取正在点击的单元格的索引路径?

ios - 有什么方法可以在 Storyboard 中添加缩写/速记/常量来代替字符串?

ios - Google Cast 设备 DidComeOnline(设备 : GCKDevice!)从未被调用 #GoogleCastSDK

ios - 数组中字符串的 UISearchBar

swift - 在特定持续时间后删除 subview

objective-c - 仅展开 NSOutlineView 中的根节点

ios - 禁用/启用 mailComposeController 中的“发送”按钮