Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument type 'customClass.Type' does not conform to expected type 'NSItemProviderWriting'

iOS 11.x Swift 4

Trying to implement a custom class for using the new drop and drag protocol and need some super-coder help. I created this class.

import UIKit
import MobileCoreServices

class CustomClass: NSObject, NSItemProviderWriting, NSItemProviderReading {

var image2D:Data?

static var readableTypeIdentifiersForItemProvider = [kUTTypeData as String]

static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> Self {
    return try self.init(itemProviderData: data, typeidentifier: kUTTypeData as String)
}

required init(itemProviderData data: Data, typeidentifier: String) throws {
    super.init() 
    image2D = data
}

static var writableTypeIdentifiersForItemProvider = [kUTTypeData as String]

func loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping (Data?, Error?) -> Void) -> Progress? {
    let data2E = image2D
    completionHandler(data2E, nil)
    return nil
}

}

It compiles, looks ok? Then I reference it with this call.

func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
         let itemProvider = NSItemProvider(object: CustomClass)
        let dragItem = UIDragItem(itemProvider: itemProvider)
        return [dragItem]
    }

And I got the error message ... Argument type 'CustomClass.Type' does not conform to expected type 'NSItemProviderWriting'...

But beyond that cannot seem to find any more clues here, there or anywhere as to how to move forward on this.

A side point I implemented one of these guys, it worked...

  itemProvider.registerDataRepresentation(forTypeIdentifier: kUTTypeJPEG as String, visibility: .all)

And I implemented one of these guys, it worked too...

itemProvider.registerFileRepresentation(forTypeIdentifier: kUTTypeJPEG as String, fileOptions: [.openInPlace], visibility: .all)

So I am thinking, the code cannot be THAT wrong... surely...

like image 863
user3069232 Avatar asked Nov 28 '17 15:11

user3069232


1 Answers

The error message is correct and your line:

let itemProvider = NSItemProvider(object: customClass)

is incorrect for the reason stated. The object parameter is expecting an instance of some class that conforms to the NSItemProviderWriting protocol. But you are passing in an actual class, not an instance of the class.

Replace customClass with an actual instance of your customClass. If this method is inside your customClass, then pass self.

let itemProvider = NSItemProvider(object: self)

BTW - this would be less confusing if your followed standard naming conventions. Class and struct names should begin with uppercase letters. Variable and method names start with lowercase letters. So your customClass should be named CustomClass.

like image 70
rmaddy Avatar answered Nov 07 '22 00:11

rmaddy