Let's go by parts!
I'm trying to implement Drag and Drop in my UICollectionViewController.
The datasource for the UICollectionView is an array of a custom Model Struct I've created.
As required I have set my collectionView.dragDelegate = self and by doing so I've implemented the required protocol function itemsForBeginning session: UIDragSession...
Here's where my problem starts:
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]
}
I cannot create a dragItem because of my model doesn't conform to type NSItemProviderWriting.
If I force a datasource to be of type String and cast the item to NSString it works, but not with my struct Model.
Does anyone know how to resolve this issue?
You should use a
class(not a struct) for yourModel, because as you suggested you have to be conform toNSItemProviderWriting(which inherits fromNSObjectProtocol):Many APIs expect subclasses of
NSObject, hence you have to use a class, Apple blog: struct vs classSo your
Modelshould be something like: