让我们分部分来吧!
我正在尝试Drag and Drop
在我的UICollectionViewController
.
的数据源UICollectionView
是我创建array
的一个自定义项。Model Struct
根据需要,我设置了我的collectionView.dragDelegate = self
,并通过这样做我实现了required protocol function
itemsForBeginning session: UIDragSession...
这是我的问题开始的地方:
struct Model {
// some variables
// Some initializations
}
var myModelDatasource: [Model] = [model1, model2, model3, ...] // it's a simple case example
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
let item = myModelDatasource[indexPath.row]
let itemProvider = NSItemProvider(object: item)
let dragItem = UIDragItem(itemProvider: itemProvider) // <-- ERROR HERE, Even If i force cast as NSItemProviderWriting
dragItem.localObject = item
return [dragItem]
}
我无法创建 adragItem
因为我的模型不符合 type NSItemProviderWriting
。
如果我强制数据源为类型String
并将项目强制转换为NSString
它有效,但不适用于我的struct Model
.
有谁知道如何解决这个问题?