参数类型“ customClass.Type”与预期的类型“ NSItemProviderWriting”不一致 [英] Argument type 'customClass.Type' does not conform to expected type 'NSItemProviderWriting'

查看:168
本文介绍了参数类型“ customClass.Type”与预期的类型“ NSItemProviderWriting”不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 11.x Swift 4

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

我收到错误消息...参数类型'CustomClass.Type'与预期的类型'NSItemProviderWriting'不符...

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)

所以我在想,代码不能e错了...肯定是...

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

推荐答案

错误消息正确且您所在的行:

The error message is correct and your line:

let itemProvider = NSItemProvider(object: customClass)

由于所述原因不正确。 object 参数期望某个类的实例符合 NSItemProviderWriting 协议。但是,您传递的是实际类,而不是类的实例。

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.

用实际实例替换 customClass customClass 的值。如果此方法在您的 customClass 内部,则传递 self

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

let itemProvider = NSItemProvider(object: self)

顺便说一句-如果遵循标准的命名约定,这将减少混乱。类和结构名应以大写字母开头。变量和方法名称以小写字母开头。因此,您的 customClass 应该命名为 CustomClass

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.

这篇关于参数类型“ customClass.Type”与预期的类型“ NSItemProviderWriting”不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆